My.Computer.FileSystem Shortcuts for VB.NET - Part IV
July 30th, 2007
This is the fourth article in my series on the My.Computer.FileSystem Namespace. In this series we’re taking a look at the functions in this namespace and rating their usefulness as compared to related NET Framework functions.
Here are the previous articles in the series:
MoveDirectory
This function obviously moves a directory from one location to another and it has 4 overloads as shown here:
My.Computer.FileSystem.MoveDirectory(SourceDir, DestDir)
My.Computer.FileSystem.MoveDirectory(SourceDir, DestDir, True)
My.Computer.FileSystem.MoveDirectory(SourceDir, DestDir, _
FileIO.UIOption.AllDialogs)
My.Computer.FileSystem.MoveDirectory(SourceDir, DestDir, _
Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, _
FileIO.UICancelOption.ThrowException)
.
It extends this System.IO Function…
System.IO.Directory.Move(SourceDir, DestDir)
…by adding the ability to choose between overwriting or not as well as offering the standard VB UI options.
Since it extends the functionality of the System.IO option, although the base is the same, I’ll give it a 3.5 out of 5.
MoveFile
MoveFile, of course, moves a file from one location to another. This function also has 4 overloads, just like MoveDirectory, as seen here:
My.Computer.FileSystem.MoveFile(SourceFile, DestFile)
My.Computer.FileSystem.MoveFile(SourceFile, DestFile, True)
My.Computer.FileSystem.MoveFile(SourceFile, DestFile, _
FileIO.UIOption.OnlyErrorDialogs)
My.Computer.FileSystem.MoveFile(SourceFile, DestFile, _
Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, _
FileIO.UICancelOption.DoNothing)
The related System.IO function…
System.IO.File.Move(SourceFile, DestFile)
…is just like its directory counterpart and is limited. Likewise, I’ll have to give it a 3.5 out of 5 rating too.
One thing to remember about using these functions, either in the My or System.IO namespaces, is that the security on a file or folder is copied to the new location.
OpenTextFieldParser
Our next function is OpenTextFieldParser. This is one of the most powerful functions in this namespace since it provides an easy way to parse a wide variety of delimited and fixed column text files. Here are some example calls to this function:
Dim MyParser As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(DelimitedFilename) Dim MyParser As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(DelimitedFilename, ",") Dim MyParser As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(DelimitedFilename, 10, 12, 16) .
I’ve written a previous article on how to use the TextFieldParser object and I’ll probably write another on it soon. If you’re doing any kind of work with delimited text files you owe it to yourself to check out this object. There is no direct equivalent in the System.IO namespace. To get the same functionality you would have to write considerable code.
I give this function, and its releated object, my highest rating 5 out of 5.
OpenTextFileReader
This function opens a FileStream for a target text file. You can optionally select an encoding method (ASCII is the default). Here are examples of the two overloads:
Dim CurrentFileAs System.IO.FileStream = My.Computer.FileSystem.OpenTextFileReader(MyTextFile) Dim CurrentFile As System.IO.FileStream = My.Computer.FileSystem.OpenTextFileReader(MyTextFile, System.Text.Encoding.UTF8) .
The System.IO function that’s closest to this one is…
Dim CurrentFile As System.IO.StreamReader = System.IO.File.OpenText(MyTextFile) .
There are differences between the two functions, obvious being the encoding type isn’t available for the System.IO function and that one returns a FileStream and the other a StreamReader. Ultimately, this means that you have to use care in selecting which method to use due to these subtle, but important, differences.
I give this function 3.5 out of 5. While it does bring some new ideas to the table, it may be of greater value in some cases to use the System.IO functions for greater consistancy.
OpenTextFileWriter
Our last function for today is OpenTextFileWriter. This function opens a StreamWriter and you can chose to overwrite or not overwrite an existing file as well as select the encoding type as shown here:
Dim CurrentFile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(MyTextFile, True) Dim CurrentFile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(MyTextFile, True, System.Text.Encoding.UTF8) .
There are several System.IO functions that you can use to achieve the same effect, however, these generally require a few extra steps to configure properly. This function provides a useful shortcut for a commonly called function so I give it a 4 out of 5.
It is important to remember encoding when working with text files. Some implementations will write out the Byte Order Mark and others don’t. For further reference, see my article, Beware of the Byte Order Mark.
This concludes Part IV of this series. I’ll be wrapping this series up with Part V during the next few days. If you’ve found this series useful (or if you didn’t) or if you have another VB.NET topic you would like me to examine, please leave me a comment.
Entry Filed under: VB.NET Tutorials
Rate This Article:









(2 votes, average: 4 out of 5)
3 Comments Add your own
1. Ken Williams | January 14th, 2008 at 6:05 pm
Excellent article!
You have explained this, in my opinion, better than Microsoft.
Thank you
Ken
Indiana USA
2. Anonymous | April 7th, 2009 at 1:35 pm
Thanks for explaining the OpenTextFieldParser. I have a feeling that it will be very beneficial in my work.
3. Anonymous | April 7th, 2009 at 1:39 pm
Sorry to double post, but I was wondering if you could you Split to accomplish the same task as OpenTextFieldParser?
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed