C# Read String Line by Line

No Comments »

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: 27% [?]

C# Convert String to Stream, and Stream to String

6 Comments »

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

Read the rest of this entry »

Popularity: 77% [?]

C# Read Text File Line-by-Line

8 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: 43% [?]

Check Valid File Path in C#

4 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: 49% [?]

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: 23% [?]

C# Copy Folder Recursively

27 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: 37% [?]

C# Read Text File into String

3 Comments »

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

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

Popularity: 55% [?]

Truncate File Path with Ellipsis

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

Popularity: 15% [?]

List Drives and Volumes from .NET

1 Comment »

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 »

Popularity: 12% [?]

Console Output from a WinForms Application

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

Popularity: 41% [?]