C# Convert Byte Array to String

2 Comments »

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[10];
// todo: populate the buffer with string data
string s = Encoding.ASCII.GetString( buffer );

Convert String to Byte Array

1 Comment »

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 );

Read File into Byte Array

1 Comment »

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 );

Set InitialDirectory for FolderBrowserDialog

2 Comments »

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:

Visual Studio 2010 and .NET Framework 4.0 Released Today

No Comments »

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 >>

Programmatically Set Decimal Places

1 Comment »

It’s easy to programmatically set the number of decimal places in a decimal, float or double that is converted to a string.  Use the ToString method with the “N” argument summed with the number of decimal places:

float value = 3.1415926F;
int decimalPlaces = 3;
string text = value.ToString( "N" + decimalPlaces );

Note that ‘decimalPlaces’ must be greater than or equal to zero, otherwise an exception will be thrown.

Graphics.MeasureString Exception: Parameter is Invalid

1 Comment »

My C# program threw an exception from the following method:

SizeF size = Graphics.MeasureString( text, font );

Unfortunately, the exception’s message was rather obscure: “Parameter is invalid.” 

After an investigation, I discovered the error occurred because the font had been previously disposed

Unfortunately, the Font class does not have the typical IsDisposed property, so there was no way to directly query the font’s disposed state.  But I searched in my code and found where I was calling the font’s Dispose method, and then incorrectly trying to use the font again in my call to the MeasureString method.

Visual Studio 2010 and .NET Framework 4 Release Candidate

1 Comment »

The Release Candidate (RC) for Visual Studio 2010 and .NET Framework 4.0 is now available to the public.  The biggest change from Beta 2 is a major improvement to Visual Studio performance, specifically as it relates to loading solutions, typing, building and debugging.  The RC includes a “go-live license” for companies that wish to deploy Visual Studio 2010 in their production environment.

Download VS 2010 and .NET 4.0 RC

Visual Studio Myth Buster

No Comments »

Do you need help convincing your boss that your company needs to upgrade to Visual Studio 2010?  Or perhaps you are looking for additional ammo in your .NET vs. Java religious wars with your programming colleagues?

Microsoft has produced a Silverlight-based “Myth Busting Matrix” for Visual Studio.  This nifty web tool details the benefits of upgrading to Visual Studio 2010 and helps dispel some widely-held myths about Visual Studio and the Microsoft .NET Framework.  You can browse all three supported versions of Visual Studio (2005, 2008 and 2010) by your areas of interest and click on the myths for more information.

Visual Studio Myth Buster

Visual Studio Myth Buster

Visual Studio 2010 Tip of the Day

1 Comment »

Zain Naboulsi, a Senior Developer Evangelist at Microsoft, has started the “Tip of the Day” series for Visual Studio 2010, taking the reins from Sara Ford.

Visual Studio 2010 Tip of the Day

« go backkeep looking »