1. Technology

Simple File Processing and VB.NET

A summary of the different ways to work with simple files

By

Updated June 22, 2008

A long time reader of About Visual Basic, who is working on getting completely up to speed in VB.NET, recently wrote and said, "What I need is some understanding of how to do simple file I/O."

Sounds like a reasonable request to me!

Part of the problem is that in VB.NET (to use a phrase from that breakthrough book, "Future Shock"), we're confronted with "overchoice". There isn't just one way to do it. There are a lot of ways. And when you go looking for help at Microsoft's web site, you end up being drowned in technical detail about just one of them.

The goal of this article is to summarize the ways that "simple file processing" can be done in VB.NET. There's not a depth of detail about any one of them, but you can see what's available and make a choice that fits your needs.

(By the way ... I'm far from certain that I've found them all. If you have a favorite technique that I haven't covered, let me know and I'll update the article.)

The basis for all of the techniques is Visual Basic 2008 Express Edition since I believe in using free software whenever you can. If you need an introduction to that, I recommend my Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial. Downloading and installing the software is covered.

All of the code discussed in this article is in a single Windows forms application as shown below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Legacy Methods

One problem my email friend had was how to process ".ini" files, "I've been reduced to using the API calls that manipulate ".ini" files because I haven't figured this out."

".ini" files are files with configuration or parameter information saved as character text. They have a specific format shown in this example borrowed from Wikipedia:

[section1]
; some comment on section1
var1 = foo
var2 = doodle
[section2]
; another comment
var1 = baz
var2 = shoodle

Unfortunately for my friend, however, Microsoft isn't doing anything to make it easier for you to use the ".ini" format. They've moved on to XML and they want you to do that too. So if you want to read the parameters from an ".ini" file directly, API calls are what you have to use. For more on using API calls to access the contents of ".ini" files, I recommend the Microsoft support article: http://support.microsoft.com/kb/75639.

The best known example is probably Win.Ini, a file that you can still find in Windows Vista today. For that reason, I've used Win.Ini from my computer to illustrate some of the subroutines. My example just reads the whole file however. Parsing the lines to dig out the parameters would need more coding.

This type of file access is usually called, "sequential file access." Here's how Microsoft describes it:

Sequential access is designed for use with plain text files. Each character in the file is assumed to represent either a text character or a text-formatting sequence, such as a newline character. Data is stored as ANSI characters.

Microsoft provides objects in the VB.NET Microsoft.VisualBasic.FileSystem namespace specifically designed to make it easier to use legacy code in VB.NET. This namespace isn't automatically imported so you should use an Imports statement whenever you use the objects. And Microsoft warns that using them, "may have an impact on performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance."

But the code to use them is a lot like good old VB6:

Imports Microsoft.VisualBasic.FileSystem

Dim FileNumber As Integer = 1
Dim FileName As String

Private Sub ReadLineInput_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles ReadLineInput.Click
   FileName = "Win.Ini"
   FileOpen(FileNumber, FileName, OpenMode.Input)
   Do Until EOF(FileNumber)
      Console.WriteLine( _
      LineInput(FileNumber))
   Loop
   FileClose(FileNumber)
End Sub

One type of 'simple file' uses Comma Separated Values. One way to process them is on the next page.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Simple File Processing and VB.NET

©2014 About.com. All rights reserved.