C# Copy Folder Recursively

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

C# Read Text File into String

9 Comments »

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

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

Truncate File Path with Ellipsis

17 Comments »

The Microsoft .NET Framework is quite comprehensive, but occasionally an obvious function slips through the cracks and you have to use InteropServices to access the Windows API.

One such obvious miss is the ability to truncate a file path. If you are drawing text and know the font and desired output size, you can use the WinForms TextRenderer class. But to truncate a file path to a specific number of characters, you need the “Shell Lightweight Utility Library” function PathCompactPathEx:

Read the rest of this entry »

List Drives and Volumes from .NET

2 Comments »

The .NET Framework v2.0 did a nice job filling many holes in the System.IO namespace, especially when it comes to managing the file system. One such addition is the DriveInfo class, which enables you to determine what drives are available, their type, capacity and available free space.

Read the rest of this entry »

Console Output from a WinForms Application

45 Comments »

You may wish to enable your WinForms application to run from a console window or command line. And when it does, you probably want to send output messages to the console window that launched your WinForms application.

Unfortunately Console.WriteLine()–the standard method of writing to the console window–by default will not work from a WinForms application. That’s because the console window that launched your WinForms application belongs to the cmd.exe process, which is separate from your WinForms application process.

Read the rest of this entry »

« go back