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
System.IO.DirectoryInfo oDirectoryInfo;
oDirectoryInfo = 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
System.IO.FileInfo oFileInfo;
oFileInfo = 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.
My.Computer.FileSystem
These are very quick IO
My.Computer.FileSystem.WriteAllText
My.Computer.FileSystem.ReadAllText
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.
System.IO.FileSystemWatcher
My.Computer.FileSystem.Drives - an enumeration of available drives
My.Computer.FileSystem.DirectoryExists - checks the presence of a directory
With System.IO.DriveInfo
.AvailableFreeSpace
.RootDirectory
.TotalSize
.TotalFreeSize
End WIth
UNC - \\system\share\directory
CopDirectory( , showUI)
showUI = UIOption.AllDialogs | OnlyErrorDialogs
System.IO.FileInfo
My.Computer.FileSystem.SpecialDirectories.Desktop
With System.Environment.SystemDirectory
.ApplicationData
.ProgramFiles
.Favourties
.System
End With
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext