C# Search/Replace in Files

35 Comments »

Sometimes you need a quick & easy way to search and replace text in a file. The following code shows how it can be done using the static methods on the Regex regular expression class. Because this sample loads the entire file contents in memory, is not appropriate for very large files.

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 »

Adding Assemblies to the Visual Studio “Add Reference” Dialog

8 Comments »

When you attempt to add an assembly reference to a Visual Studio project, the Add Reference dialog appears with a list of registered global assemblies in the .NET tab:

Add Your Assembly to Visual Studio

Unfortunately, adding your assembly to the Global Assembly Cache (GAC) does NOT make it automatically appear in the Visual Studio list of installed assemblies; you must add your assembly manually as follows:

Read the rest of this entry »

Determine the Version of a Loaded .NET Assembly

1 Comment »

Sometimes you need to know which version of an assembly was loaded by your .NET application. The following code snippet makes it easy:

Read the rest of this entry »

System Sounds Made Easy

4 Comments »

Playing the default Windows sounds from C# used to require InteropServices and system calls. Fortunately, .NET 2.0 includes a new System.Media namespace with three classes that simplify playing system sounds and audio files:

Read the rest of this entry »

C# Alias: ‘Using’ Directive

1 Comment »

One of the things I miss about C++ is the #define keyword that allows you to create an abbreviated alias for a long type name. Fortunately, C# provides a way to alias a long namespace or class name while retaining full type-checking.

To alias a namespace or class name, use the using directive to define the alias as shown in the sample code below. Then you can use the alias anywhere you would normally use the class name or namespace. The scope of a using directive is limited to the file in which it appears.

Read the rest of this entry »

Embedded Image Resources

15 Comments »

If you use images in a .NET application, chances are you will find it more convenient to embed those images as resources in your project, rather than leaving them as separate files and trying to locate and load the images from disk when the application runs.

Read the rest of this entry »

DoDragDrop is Synchronous

5 Comments »

The DoDragDrop method on a control is synchronous. This means that when you call DoDragDrop to start a drag operation, the program will not return and execute the rest of your handler code until the user 1) drops the data she is dragging, or 2) cancels the drag operation. This is one of those little facts that you need to file away in your brain, as it could have a profound effect on your drag & drop logic.

Read the rest of this entry »

Prevent Beep for TextBox Enter

4 Comments »

Typically when you press the Enter key while typing in a TextBox control, you will hear the computer beep.

To prevent this beep, handle the Enter key in the KeyPress event, and set the Handled property to true. For example:

Read the rest of this entry »

« go back