C# File Processing - Lessons

 

Introduction to File Processing

Up to now no matter how much data we entered into our text boxes, when the power was turn off everything entered was gone. No more. This section is going to teach us how we can take our data, save it somewhere safe so we can turn off our machines, go home, have dinner, get some rest, and come back the next day and find our previous days work waiting for us where we left it.

Write to a File - The Basics

You have a lot of options when saving data.  Because of all those options it’s easy to get confused when first starting out.  To get a good understanding of what’s going on it best to learn how to just write one record to disk.  Once we know how to do that, we can explore how to handle large files.  And you’ll be doing that in no time at all.

Write to a File - Using a Loop

It’s important to understand all the pieces when writing to a disk file.  And it’s easier to explain when we just use one record.  But 9,999 times out of 10,000 we are going to want to write a whole bunch of records at once.  And this is an excellent time to make use of the loop.

This video will show you how to use a loop when writing a file.  When it comes to reading a file, the loop is just as useful. 

Reading a File - The Basics

The interesting thing about reading a file is that we can read from all sorts of files.  Text files.  Spreadsheet data.  Even database records.  We need to learn a little more about strings before we go there but there is nothing stopping us from doing quite a bit with files now.

So, let’s examine how we read data from a file.   This next video is going to read one record from a file.  Once that’s done, we’ll add a few records.  We’ll even blow up our program a bit to show how to prevent errors.  And we’ll end using a loop to read a file.  Fantastic.

OpenFileDialog and SaveFileDialog Controls

The C# programming language has two objects we can use to give the user the ability to specify where a file should be written, where it can be found, and what it should be called.  These objects are called OpenFileDialog and SaveFileDialog.  They have properties just like a label, a button or a text box.  But they don’t go on the form.  They go in an invisible tray (to the user) just under the form.  Once placed there they are part of your program.

The following video will show you how to use the SaveFileDialog control.  Most of the code you have recently written to save a file to disk can be used unchanged.  We only need to change how the file name is accessed.

Reading a Tab Delimited File

Getting data from external sources is just a natural. That’s why reading text files is so important to a programming language. There are thousands of applications that make their data available as a text file. You can extract data from an Access database as a text file. You can extract spreadsheet data as a text file. And that’s just two applications. The list is endless.

In order to use the data from these applications you need to know how the data is structured. In many cases you are given the option for specifying the structure of the data.

Once the data is extracted, using your program language (in this case C#) you can read that data and place the data into individual variables, list boxes and arrays. With that done your program can use the data just like data entered via a text box. Except, this only took seconds. There was no extensive data entry operation going on.

Well, how does the program do all this. That’s what this next video is all about.