Author Archives: Rod Stephens

Use a TextFieldParser to read delimited data in C#

This example uses a TextFieldParser object to parse a data file that contains name and address data. The data contains fields delimited by commas and semi-colons. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program … Continue reading

Posted in classes, database, files, parsing | Tagged , , , , , , , , , , , , , | 2 Comments

Suspend or hibernate the system in C#

The Application.SetSuspendState method lets an application make the system suspend or hibernate. A suspended system enters a low power mode. A hibernating system saves its memory contents to disk and then enters low power mode. Because it doesn’t need to … Continue reading

Posted in system | Tagged , , , , , , , , , , , , | Leave a comment

Explore the Visual Studio Image Library in C#

This example explains where you can find standard images in the Visual Studio Image Library that you can use in your applications. The example Give transparent backgrounds to images displayed on buttons in C# explains how to make some pixels … Continue reading

Posted in controls, graphics | Tagged , , , , , , , , , , , , | Leave a comment

Give transparent backgrounds to images displayed on buttons in C#

This example shows how to give transparent backgrounds to the images displayed on a button. If you give buttons images that have transparent backgrounds, then the button’s background can show through. If the images don’t have transparent backgrounds, the result … Continue reading

Posted in controls, graphics, image processing | Tagged , , , , , , , , , , , | Leave a comment

Easily draw rotated text on a form in C#

The example Draw rotated text in C# shows how to draw rotated text. Unfortunately that method requires you to use code at run time to position the text. It’s not too hard but it’s inconvenient if you’re trying to draw … Continue reading

Posted in fonts, forms, graphics | Tagged , , , , , , , , , , , , | 1 Comment

Randomly change the desktop picture in C#

The example Set the Windows desktop picture in C# shows how to display an image on the desktop. This example periodically changes the desktop picture to a randomly selected image file picked from a directory. Note that recent versions of … Continue reading

Posted in API, graphics, image processing, interoperability, system | Tagged , , , , , , , , , , , , , , , | Leave a comment

Set the Windows desktop picture in C#

This example sets the Windows desktop picture much as you can manually in Windows 10. (I can’t guarantee that it will work without any changes in other versions of Windows.) Exactly how you set the desktop picture manually depends on … Continue reading

Posted in API, graphics, interoperability, system | Tagged , , , , , , , , , , , , , , , | Leave a comment

Convert RTF and TXT files into DOCX files in C#

First add a reference to Microsoft.Office.Interop.Word. To make using the library easier, add this using statement: using Word = Microsoft.Office.Interop.Word; When you enter the input and output file names and click Convert, the program uses the following code to open … Continue reading

Posted in files, interoperability, Office, Word | Tagged , , , , , , , , , , , , , | Leave a comment

Remove non-printable ASCII characters from a string in C#

The following TrimNonAscii extension method removes the non-printable ASCII characters from a string. public static string TrimNonAscii(this string value) { string pattern = “[^ -~]+”; Regex reg_exp = new Regex(pattern); return reg_exp.Replace(value, “”); } In ASCII, the printable characters lie … Continue reading

Posted in extension methods, strings | Tagged , , , , , , , , , , , , | 7 Comments

Use “banker’s rounding” and “normal rounding” in C#

By default the Math.Round method uses “banker’s rounding.” In banker’s rounding a number with a final digit of 5 is rounded to the nearest even number rather than to the next larger number as you might expect. The idea is … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , , , | Leave a comment