My.Computer.FileSystem Shortcuts for VB.NET - Part V
August 2nd, 2007
This is the fifth and final article in my series on the My.Computer.FileSystem Namespace. This series takes a look at this namespace’s functions and rates their usefulness as compared to similar NET Framework functions.
Here are the previous articles in the series:
ReadAllBytes
This function reads all the bytes from a file and returns a Byte array. It is functionally identical to the System.IO function also shown here:
Dim FileBytes() As Byte = My.Computer.FileSystem.ReadAllBytes(BinFile) Dim FileBytes() As Byte = System.IO.File.ReadAllBytes(BinFile) .
Since it is an exact duplicate of the System.IO function, I give this function 2 out of 5.
ReadAllText
This function reads a text file and returns a string. It too is functionally identical to the System.IO function also shown here:
Dim FileText As String = My.Computer.FileSystem.ReadAllText(TextFile) Dim FileText As String = System.IO.File.ReadAllText(BinFile) .
To make matters worse, the System.IO function is better since it has an overload that allows you to specify the encoding type of the text file. Since it reduces functionality, I give this function 1 out of 5.
RenameDirectory
This function renames a directory to a new name, but, unlike MoveDirectory it does not move it. A very subtle but important difference. The second parameter must be a string with no relative or absolute path information. Otherwise an exception is thrown.
Dim CurrentDirName As String = "C:\docs\test1" 'The following line would cause an exception 'Dim NewDirName As String = "C:\docs\test2" 'This line is OK Dim NewDirName As String = "test2" My.Computer.FileSystem.RenameDirectory(CurrentDirName, NewDirName)
Since this function does bring something unique but it’s a little confusing to use since it could be mistaken for Move, so I’ll only give it 3 out of 5.
RenameFile
This function renames a file to a new name but does not move it, just like we saw in the RenameDirectory function above.
Dim CurrentFileName As String = "C:\docs\test1.txt" 'The following lines would cause exceptions 'Dim NewFileName As String = "C:\docs\test2.txt" 'Dim NewFileName As String = "D:\backupdocs\test1.text" 'This line is OK Dim NewFileName As String = "test2.txt" My.Computer.FileSystem.RenameFile(CurrentFileName, NewFileName)
Also like the RenameDirectory, I’ll give this one a 3 out of 5 for the same reason.
WriteAllBytes
As you can guess from the name, this function writes the bytes in a byte array to a specified file. It can either overwrite the existing file or append to it.
My.Computer.FileSystem.WriteAllBytes(FileToWrite, FileBytes(), False)
The ability to append or overwrite enhances the base System.IO.File method of the same name. Therefore, I’ll give it 4 out of 5.
WriteAllText
Our last function writes text to the target file and you may either overwrite or append an existing file. An overload allows you to also select the encoding method:
My.Computer.FileSystem.WriteAllText(FileToWrite, FileText, True) My.Computer.FileSystem.WriteAllText(FileToWrite, FileText, True, Encoding.ASCII) .
While the ability to overwrite or append is a nice enhancement over the similar System.IO function, there is a nasty little ‘gotcha’ in this routine that I wrote about in my Beware of the Byte Order Mark article. It seems that the default encoding for this function is UTF-8 and, unlike other .NET text file writing functions using this encoding, this one writes out the Byte Order Mark. Because of this non-standard behavior, I have to give this one a dismal 1 out of 5.
Recap
Here’s a quick recap of the score all of the functions reviewed in this article series:
5 Stars
FindInFiles
OpenTextFieldParser
4 Stars
CopyDirectory
GetDirectories
WriteAllBytes
GetName
GetParentPath
GetTempFileName
OpenTextFileWriter
3 or 3.5 Stars
CopyFile
DeleteDirectory
MoveDirectory
MoveFile
OpenTextFileReader
RenameDirectory
RenameFile
2 Stars
CombinePath
CreateDirectory
DirectoryExists
FileExists
GetDirectoryInfo
GetDriveInfo
GetFileInfo
ReadAllBytes
1 Star
ReadAllText
WriteAllText
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:











Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed