Files & Directories

.NET Classes

There are five classes in the System.IO namespace that are very useful:
Directory - Contains shared methods that let you enumerate and manipulate directories
File - Contains shared methods that let you enumerate and manipulate files
DirectoryInfo - Represents an individual directory and exposes methods to query its attributes and manipulate them
FileInfo - Represent an individual file and exposes methods to query its attributes and manipulate them
Path - Contains shared methods that let you manipulate path information
This namespace also provides classes to allow synchronous and asynchronous reading from and writing to data strreams and files.
FileStream -
StreamReader -
StreamWriter -

Directory Class

File Class

This class can be used as an interface to a particular file but not to the actual data within the file.
You can use this class to open files but if you want to read and write the contents then you must use the FileStream class.
One drawback of using these methods is that the text streams returned use the UTF-8 encoding.
This is fine for a lot of applications but if the file contains any characters that are not recognised then these are usually just ignored.
Using the StreamReader and StreamWriter constructors directly avoids any possible encoding issues.
These classes also allow you to set the buffer size (useful for optimisation) as well as being able to specify the byte order (to guarantee the correct encoding).

DirectoryInfo Class

The DirectoryInfo class represents a single directory
This class inherits from the FileSystemInfo virtual class
You can create a reference to a DirectoryInfo object by using its constructor method

Dim objDirectoryInfo As System.IO.DirectoryInfo 
   objDirectoryInfo = New System.IO.DirectoryInfo("C:\Temp\")

FileInfo Class

The FileInfo class represents a single file
This class inherits from the FileSystemInfo virtual class
You can create a reference to a FileInfo object by using its constructor method

Dim objFileInfo As System.IO.FileInfo 
   objFileInfo = New System.IO.FileInfo("C:\Temp\textfile.txt")

FileMode enumeration

FileMode is an enumeration of values used to specify the type of file access.

FileMode.CreateNew - creates a new file assuming it doesn't already exist.
FileMode.Open -
FileMode.Append -
FileMode.OpenOrCreate - Opens a file if it exists or creates it if it doesn't exist
FileMode.Create - Creates a new file silently overwriting if it already exists.

FileAccess enumeration

FileShare enumeration

Once you have a filestream of the data you want you need to attach it to a Reader or Writer to be able to use it.
There are two options:
Plain Text - Use StreamReader and StreamWriter classes
Binary Data - Use BinaryReader and BinaryWriter classes

The Reader classes are for retrieving data from a file.
The Writer classes are for sending data to a file.


These are very quick IO

VB.Net Functions

FileOpen - Opens a text file for input and output
LineInput - Reads a line of input from the text file
EOF - Checks for the end of the text file
FileClose - Closes the text file

Dim sText As String 
Dim sLineOfText As String
FileOpen(1, "C:\Temp\textfile.txt", OpenMode.Input)
Do Until EOF(1)
   sLineOfText = LineInput(1)
   sText = sText & sLineOfText & vbCrLf

Binary Files

Binary filea are not used that much anymore although it can still exist in legacy systems.
It is sometimes more efficient to store information in a random access binary file than in a database.


My.Computer.FileSystem.Drives - an enumeration of available drives
My.Computer.FileSystem.DirectoryExists - checks the presence of a directory

With System.IO.DriveInfo
End WIth

UNC - \\system\share\directory

CopDirectory( , showUI)
showUI = UIOption.AllDialogs | OnlyErrorDialogs


With System.Environment.SystemDirectory
End With

© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopNext