Get Temporary Directory

No Comments »

To get the path of the current user's temporary folder, call the GetTempPath method in the System.IO namespace:

string tempPath = System.IO.Path.GetTempPath();

On Windows Vista and 7, this method will return the following path:

C:\Users\UserName\AppData\Local\Temp\

Popularity: 2% [?]

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

Popularity: 4% [?]

Set InitialDirectory for FolderBrowserDialog

1 Comment »

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:

Popularity: 3% [?]

C# Read String Line by Line

1 Comment »

It’s easy to read a string one line at a time.  Here is a console program that demonstrates how:

Read the rest of this entry »

Popularity: 24% [?]

C# Convert String to Stream, and Stream to String

8 Comments »

It’s fairly easy to convert a C# String to a Stream and vice-versa.

Read the rest of this entry »

Popularity: 93% [?]

C# Read Text File Line-by-Line

13 Comments »

Here is the code to read a text file from disk one line at a time into a string.  This code ensures the file exists and properly closes the file if an exception occurs.

Read the rest of this entry »

Popularity: 46% [?]

Check Valid File Path in C#

5 Comments »

It's not a trivial exercise to validate a file path on a Windows PC.  There are a few special cases depending on the file system and operating subsystem:

Read the rest of this entry »

Popularity: 42% [?]

Display an RTF File that's a C# Embedded Resource

No Comments »

It's easy to display an RTF file — that was embedded as a resource in a C# program — in a Windows Form RichTextControl.

Read the rest of this entry »

Popularity: 17% [?]

C# Copy Folder Recursively

30 Comments »

Sadly there is no built-in function in System.IO that will copy a folder and its contents.  Following is a simple recursive algorithm that copies a folder, its sub-folders and files, creating the destination folder if needed.  For simplicity, there is no error handling; an exception will throw if anything goes wrong, such as null or invalid paths or if the destination files already exist.

Read the rest of this entry »

Popularity: 33% [?]

C# Read Text File into String

5 Comments »

Here is the easiest way to read an entire text file into a C# string:

string s = System.IO.File.ReadAllText( path );

Popularity: 51% [?]

keep looking »