Category Archives: files

Safely manage documents in C#

This is a pretty involved example that shows how to safely manage documents. The pieces are all simple but there are a lot of tightly integrated pieces. This example lets you create, edit, open, and save files in text and … Continue reading

Posted in files, forms, user interface | Tagged , , , , , , , , , , , , , , , , | 1 Comment

Parse file sizes in KB, MB, GB, and so forth in C#

The example Format file sizes in KB, MB, GB, and so forth in C# shows how to convert a number into a string formatted in KB, MB, etc. This example does the opposite: it parses a value such as “1.23 … Continue reading

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

Make a simple event logger in C#

Usually the debugger lets you examine how a program works quite effectively, but sometimes it’s useful to use an event logger to record events for later study. For example, when you’re working with mouse events, stopping execution at a break … Continue reading

Posted in classes, debugging, files | Tagged , , , , , , , , , , | Leave a comment

Make a hangman game in C#

Special thanks to Jeff Scarterfield for the skeleton drawing used by the program. This example builds a simple hangman game that uses the dictionary created by the example Use LINQ to select words of certain lengths from a file in … Continue reading

Posted in algorithms, files, games, graphics, strings | Tagged , , , , , , , , , , , , | Leave a comment

Use LINQ to select words of certain lengths from a file in C#

This example uses LINQ to read a file, remove unwanted characters, select words of a specified length, and save the result in a new file. Recently I needed a big word list so I searched around for public domain dictionaries. … Continue reading

Posted in algorithms, files, LINQ | Tagged , , , , , , , , , , , | 2 Comments

Use a TextFieldParser to read fixed-width data in C#

This example uses a TextFieldParser object to load fixed-width data from a file that contains names and addresses. Each field has a fixed width. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program should discard … Continue reading

Posted in classes, database, files, parsing | Tagged , , , , , , , , , , , , , , , | Leave a comment

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

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

See what processes have a file locked in C#

In older versions of Windows, it was impossible to determine what processes had a file locked. The system kept track of the number of locks on a file and processes were responsible for incrementing and decrementing the count to represent … Continue reading

Posted in files, system | Tagged , , , , , , , , , | 1 Comment

See if a file is locked in C#

Some applications lock files so you cannot write, read, delete, or otherwise mess with them. For example, when you open a file in Microsoft Word, it locks the file so you cannot delete it or open it for writing with … Continue reading

Posted in files, system | Tagged , , , , , , , , , , , , , , | 3 Comments