When you download XML text from the Web, you may find “garbage characters” in the start of your XML string. For example, I encountered this result when I downloaded an XML string using WebClient.DownloadString method:
What you are likely seeing is a Byte Order Mark (BOM), which is a Unicode character that indicates the endian-ness (byte order) of a text file or stream. The BOM is optional and will appear at the start of the text stream, if at all. The BOM may also indicate in which of the several Unicode representations the text is encoded.
Read the rest of this entry »
It’s easy to convert a byte array to a string. For an ASCII string, use the Encoding.ASCII.GetString static method:
byte buffer = new byte;
// todo: populate the buffer with string data
string s = Encoding.ASCII.GetString( buffer );
It’s easy to convert a string to a byte array. For an ASCII string, use the Encoding.ASCII.GetBytes static method:
string s = "Test String";
byte byteArray = Encoding.ASCII.GetBytes( s );
It’s easy to read a file into a byte array. Just use the File.ReadAllBytes static method. This opens a binary file in read-only mode, reads the contents of the file into a byte array, and then closes the file.
string filePath = @"C:test.doc";
byte byteArray = File.ReadAllBytes( filePath );
The .NET FolderBrowserDialog class does not have an InitialDirectory property like the OpenFileDialog class. But fortunately, it’s quite easy to set an initial folder in the FolderBrowserDialog:
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.RootFolder = Environment.SpecialFolder.Desktop;
dialog.SelectedPath = @"C:Program Files";
if (dialog.ShowDialog() == DialogResult.OK)
Console.WriteLine( dialog.SelectedPath );
Note that you can choose other RootFolder’s like MyComputer and MyDocuments. The SelectedPath must be a child of the RootFolder for this to work.
Given the sample code above, the dialog may look like this:
Microsoft is releasing Visual Studio 2010, .NET Framework 4.0, and Silverlight 4 at the Visual Studio Developer Conference in Las Vegas. VS 2010 and .NET 4 are available today, and Silverlight 4 will be available to download later this week.
Read more at DevTopics >>