System.IO
DriveInfo
Compression
System.IO.Compression
Directory
System.IO.Directory
Methods
| CreateDirectory | Creates the specified directory and all the directories on its path if necessary |
| Delete | Deletes a directory and optionally all its subdirectories |
| Move | Moves a directory |
| GetFileSystemEntries | Returns a string array will all the files and directories in a directory, optionally filtered by the specified criteria |
| GetFiles | Returns a string array will all the files in a directory, optionally filtered by the specified criteria |
| GetDirectories | Returns a string array will all the subdirectories in a directory, optionally filtered by the specified criteria |
| GetLogicalDrives | Returns a string array with all the logical drives in the system |
| GetCreationTime | Returns the creation time of the directory |
| GetCurrentDirectory | Returns the current directory (string) - equivalent to ChDrive, ChDir and CurDir functions |
| GetLastAccessTime | Returns the last access time of the directory |
| GetLastWriteTime | Returns the last write time of a directory |
| GetParent | Returns the parent directory as a DirectoryInfo object |
| GetDirectoryRoot | Returns the root directory for the specified path (string) |
| Exists | Returns True if the directory exists |
| SetCreatiionTime | Sets the creation time of the directory |
| SetCurrentDirectory | Sets the current directory |
| SetLastWriteTime | Sets the last write time of the directory |
| SetLastAccessTime | Sets the last access time of the directory |
DirectoryInfo
System.IO.DirectoryInfo
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\");
Methods
This class exposes instance methods for creating, moving and enumerating through directories, their files and their sub directories.
| Name | Returns the name of the directory |
| FullName | Returns the full name including its full path |
| Extension | Returns the extension of the directory |
| Exists | Returns true if the directory exists |
| Attributes | Sets or returns the attributes of the directory as a bit-coded FileAttrbutes value ? |
| CreationTime | sets or returns the creation time of the directory (Date value) |
| LastWriteTime | Sets or returns the last write time for the directory (Date value) |
| LastAccessTime | Sets or returns the last access time for the directory (Date value) |
| Refresh | Refreshes the properties of this object |
| Parent | Returns the DirectoryInfo object for the parent directory |
| Root | Returns the DirectoryInfo object for the root directory |
| Create | Creates the directory |
| MoveTo(destpath) | Moves the directory to another path |
| Delete([recursive]) | Deletes the directory |
| CreateSubDirectory(path) | Creates a subdirectory and returns the corresponding DirectoryInfo object |
| GetDirectories([filespec]) | Returns information about the subdirectories as an array of DirectoryInfo objects |
| GetFiles([filespec]) | Returns information about the files in the directory as an array of FileInfo objects |
| GetFileSystemInfos | Returns information about the files and subdirectories as an array of FileSystemInfo objects |
if (objfileinfo.Attributes And IO.FileAttributes.Hidden) <> _
IO.FileAttributes.Hidden)
{
}
File
System.IO.File
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).
Methods
| AppendAllLines | Opens a file and appends lines to the end of the file and closes the file. |
| AppendAllLinesAsync | Asychronously opens a file and appends lines to the end of the file and closes the file. |
| AppendAllText | Opens a file and appends a string to the end of the file and then closes the file. |
| AppendAllTextAsync | Asychronously opens a file and appends a string to the end of the file and then closes the file. |
| AppendText | Opens a text file in append mode returning a StreamWriter object |
| Copy | Copies a file to a new destination |
| Create | Creates and opens a file returning a FileStream object |
| CreateText | Creates a text file returning a StreamWriter object |
| Delete | Deletes a file |
| Exists | Returns True if the file exists |
| GetAttributes | Returns the attributes of a file |
| GetCreationTime | Returns the creation time of the file |
| GetLastAccessTime | Returns the last access time of the file |
| GetLastWriteTime | Returns the last write time of a file |
| Move | Moves a file |
| Open | Opens a file with a specified mode, access and share returning a FileStream object |
| OpenRead | Opens a file in read mode returning a FileStream object |
| OpenText | Opens a text file for reading returning a StreamReader object |
| OpenWrite | Opens a file in write mode returning a FileStream object |
| ReadAllBytes | Opens a binary file and reads the contents into a byte array and then closes the file. |
| ReadAllBytesAsync | Aschronously opens a binary file and reads the contents into a byte array and then closes the file. |
| ReadAllLines | Opens a text file and reads all the lines of the file and then closes the file. |
| ReadAllLinesAsync | Asynchronously opens a text file and reads all the lines of the file and then closes the file. |
| ReadAllText | Opens a text file and reads all the text in the file and then closes the file. |
| ReadAllTextAsync | Asynchronously opens a text file and reads all the text in the file and closes the file. |
| ReadLines | Reads all the lines of a file. |
| Replace | Replaces the contents os a specified file with the contents of another file, deleting the original file and creating a backup of the replaced file. |
| SetAttributes | Sets the attributes for a file |
| SetCreatiionTime | Sets the creation time of the file |
| SetLastAccessTime | Sets the last access time of the file |
| SetLastWriteTime | Sets the last write time of the file |
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.
Returns a DateTime telling you when the file was created.
System.IO.File.GetCreationTime(path);
System.IO.File.SetCreatiionTime(path, datetime);
System.IO.File.GetLastAccessTime(path);
System.IO.File.SetLastAccessTime(path, datetime);
System.IO.File.GetLastWriteTime(path);
System.IO.File.SetLastWriteTime(path, datetime);
System.IO.File.Exists(path);
Creates and opens a file returning a FileStream object that is initialised for both reading and writing.
The FileStream is positioned at the beginning of the file.
If the file already exists then the file is truncated and any existing data is lost.
You can also specify the buffer size.
System.IO.File.Create(file [,buffersize]);
Creates a text file and opens it for reading. This returns a StreamWriter object.
If the file you want to create already exists then it is overwritten.
System.IO.File.CreateText(file);
Deletes an existing file
This method will throw an exception if the file is in use or if you do not have access (or permission) to delete it.
It will not throw an exception if the file does not exist.
System.IO.File.Delete(path);
string[] delList = System.IO.Directory.GetFiles(folder, "*.png");
foreach (string filename in delList)
{
System.IO.File.Delete(filename);
}
Opens a file with a specified mode, access and share returning a FileStream object
The sharing mode lets you control whether or not other processes can read from or write to the file while you have it open.
A lot of the arguments correspond to overloads of the FileStream constructor.
This method gives you greater (than the Create, OpenRead and OpenWrite methods) when creating new files.
If you want total control when opening and creating files then you should use the FileStream constructor.
System.IO.File.Open(file [,mode [,access [,share ]]])
System.IO.File.Open("C:\temp\file1.txt", _
FileMode.Append | Create | CreateNew | Open | OpenOrCreate | Truncate, _
FileAccess.Read | Write | ReadWrite, _
FileShare.None | ReadWrite | Read | Write | Inheritable
Opens an existing file in read mode returning a FileStream object
The file is opened using the default sharing mode
The FileStream is positioned at the beginning of the file.
System.IO.File.OpenRead(file)
System.IO.FileStream oFileStream;
oFileStream = System.IO.File.OpenRead("C:\Temp\textfile.txt");
Opens a file in write mode returning a FileStream object
Any existing data in the file will be overwritten.
If the file you are trying to open does not exist then it will be created.
System.IO.File.OpenWrite(file);
Opens a text file for reading returning a StreamReader object
This method returns a StreamReader that is positioned at the beginning of the file.
System.IO.File.OpenText(file);
Copies a file from one location to another.
This method will fail if the file already exists and you have not explicitly stated to overwrite the file.
You also have the option to overwrite the file it it already exists.
System.IO.File.Copy(source, dest [,overwrite]);
System.IO.File.Copy("file1.txt","file2.txt",false);
System.IO.File.Copy("file1.txt","file2.txt",true);
This method can be used if you want to append data to a text file.
Opens an existing text file in append mode returning a StreamWriter object
If the file you want to open does not exist then a new file is created.
System.IO.File.AppendText(file);
Moves a file to a different location.
if the file is being moved to a different location on the same Volumen then the file is not actually moved and deleted but is just renamed.
This method will also not throw an exception if the source and dest names are the same.
System.IO.File.Move(source, dest)
GetAttributes
Returns the attributes of a file
This returns a FileAttributes enumeration that identifies the file's attributes.
FileAttributes has a FlagsAttribute that allows bitwise combination of its members so that a file can have multiple sttributes.
System.IO.File.GetAttributes(file)
If you want to determine if a file has a particular attribute you have to test for that using the boolean AND operator.
System.IO.File.FileAttributes oFileAttributes;
oFileAttributes = System.IO.File.GetAttributes("file1.txt");
if (oFileAttributes && System.IO.File.FileAttributes.ReadOnly) <> 0)
{
}
SetAttributes
Sets the attributes for a file
System.IO.File.SetAttributes(file, attributes)
FileInfo
System.IO.FileInfo
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")
This class provides instance methods for manipulating files.
System.IO.FileInfo oFileInfo;
oFileInfo = oDirectoryInfo.GetFiles;
Methods
| AppendText | Opens the text file in append mode and returns a StreamWriter object |
| CopyTo | Copies the file to another path |
| Create | Creates the file |
| CreateText | Creates a text file and returns a StreamWriter object |
| Decrypt | |
| Delete | Deletes the file |
| Encypt | |
| MoveTo | Moves the file to another path |
| Open | Opens the current file with specified mode, access and share, returns a FileStream object |
| OpenRead | Opens the file in read mode and returns FileStream object |
| OpenText | Opens the current file in read mode and returns a StreamReader object |
| OpenWrite | Opens the file in write mode and returns FileStream object |
| Replace |
Properties
| Directory | Returns the DirectoryInfo object for the parent directory |
| DirectoryName | Returns the name of the parent directory |
| Exists | Returns true if the file exists |
| IsReadOnly | |
| Length | Returns the length of the file |
| Name | Returns the name of the file |
Moves the file to another path
MoveTo(destpath)
Copies the file to another path
CopyTo(destfile [,overwrite])
Opens the current file with specified mode, access and share, returns a FileStream object
Open(mode [,access [,share]]])
Opens the file in read mode and returns FileStream object
IO.FileAttributes.Archive - 32
IO.FileAttributes.Compressed - 2048
IO.FileAttributes.Device - 64
IO.FileAttributes.Directory = 16
IO.FileAttributes.Encypted - 16384
IO.FileAttributes.Hidden - 2
IO.FileAttributes.Normal - 128
IO.FileAttributes.NotContentIndexed - 8192
IO.FileAttributes.Offline - 4096
IO.FileAttributes.ReadOnly - 1
IO.FileAttributes.ReparsePoint - 1024
IO.FileAttributes.SparseFile - 512
IO.FileAttributes.System - 4
IO.FileAttributes.Temporary - 256
System.IO.Path
string filename = @"C:\test\myfile.txt";
string path = @"c:\text\";
string result;
result = System.IO.Path.GetFileName(filename);
Console.WriteLine("GetFileName ('{0}') returns '{1}'", _
filename, result);
result = System.IO.Path.GetFileName(path);
Console.WriteLine("GetFileName ('{0}') returns '{1}'", _
path, result);
Stream
System.IO.Stream
You should never create a Stream Object directly
The Stream abstract class represents a sequence of bytes going to or coming from a storage medium.
The storage medium could be a file or a physical or virtual device.
You can have file streams, memory streams, network streams and tape streams.
The basic operations for a stream are read, write and seek.
Before you can do anything with the stream you have to attach it to a reader or writer.
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("");
FileStream
System.IO.FileStream
Use the FileStream class to read from, write to, open, and close files on a file system
StreamReader and StreamWriter
The generic stream objects can read and write only individual bytes or groups of bytes.
It is often easier to work with more structured data such as lines of text or a specific value.
The StreamReader and StreamWriter classes allow you to work with strings of text in ANSI format.
The StreamReader and StreamWriter classes can work in conjunction with an Encoder object which determines how characters are encoded in the stream
System.IO.MemoryStream
System.IO.PipeStream
System.IO.BufferedStream
System.IO.UnmanagedMemoryStream
System.IO.Compression.DeflateStream
System.IO.Compression.GZipStream
System.Printing.PrintQueueStream
System.Data.SqlTypes.SqlFileStream
System.Net.Security.AuthenticatedStream
System.Net.Sockets.NetworkStream
System.Security.Cryptography.CryptoStream
FileStream
System.IO.FileStream
Use the FileStream class to read from, write to, open, and close files on a file system
This is one of the classes derived from the Stream class.
File streams are buffered
Data is not written to a file when you write to the FileStream.
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
| BeginRead | start an asynchronous operation on the stream |
| BeginWrite | start an asynchronous operation on the stream |
| Close | Closes the stream |
| EndRead | To find out how many bytes were actually written |
| Flush | Empties the buferred stream and ensures all the contents are written |
| Handle | Returns the operating system file handle |
| Length | Returns the total size of the stream. You can change the length using SetLength |
| Lock | Lock a portion of the file |
| Position | determines the current position in the stream. You can change the position using Seek. |
| Read | Reads a number of bytes from the specified position into a Byte array. The ReadByte method reads and returns a single byte. |
| SetLength | Trims or extends the underlying file. |
| Unlock | Unlock a portion of the file |
| Write | Writes a number of bytes from an array into the stream. The WriteByte can write a single byte. |
You can check which operations are allowed by using the CanRead, CanWrite and CanSeek properties.
System.IO.FileStream oFileStream;
oFileStream = System.IO.FileStream("C:\Temp\textfile.txt",
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.Read);
if (oFileStream.CanRead == true)
{
}
System.IO.FileMode.Append | Create | CreateNew | Open | OpenOrCreate | Truncate
System.IO.FileAccess.Read | Write | ReadWrite
System.IO.FileShare.None | ReadWrite | Read | Write | Inheritable
This is another way of obtaining a FileStream object.
System.IO.FileStream oFileStream;
oFileStream = System.IO.File.OpenRead("C:\Temp\textfile.txt");
StreamReader
System.IO.StreamReader
You use a StreamReader object to read from a text file.
There are a number of ways you can obtain the StreamReader object
The fifth line, lets the system decide the best type of encoding
System.IO.StreamReader oStreamReader;
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt");
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt", System.Text.Encoding.ASCII);
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt", System.Text.Encoding.Unicode);
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt", true);
Using a FileStream
System.IO.StreamReader oStreamReader;
System.IO.FileStream oFileStream;
oFileStream = new System.IO.FileStream(@"C:\\Temp\\textfile.txt", FileMode.Open);
oStreamReader = new System.IO.StreamReader(objFileStream);
Using a Stream
This lets you specify the mode, access and share
System.IO.StreamReader oStreamReader;
System.IO.Stream oStream;
oStream = System.IO.File.Open(@"C:\\Temp\\textfile.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
oStreamReader = new System.IO.StreamReader(oStream, System.Text.Encoding.UTF8);
Using a FileInfo
System.IO.StreamReader oStreamReader;
System.IO.FileInfo oFileInfo;
oFileInfo = new System.IO.FileInfo(@"C:\\Temp\\textfile.txt");
oStreamReader = objFileInfo.OpenText
Using File.OpenText
System.IO.StreamReader oStreamReader;
oStreamReader = System.IO.File.OpenText(@"C:\\Temp\\textfile.txt");
This can also be used to read all the remaining characters.
string sWholeFile;
// async
using (oStreamReader)
{
sWholeFile = await oStreamReader.ReadToEndAsync();
}
// sync
sWholeFile = objStreamReader.ReadToEnd;
string sLine;
string s = String.Empty;
while( (s = oStreamReader.ReadLine() ) != null);
{
}
Read a Single Character
string sCharacter;
sCharacter = objStreamReader.Read;
The Peek method actually returns the code of the next character in the stream without actually extracting it, or it returns -1 if there are no more characters.
This method is used to test for the end of the file.
while (oStreamReader.Peek = -1)
{
read the stream
}
StreamWriter
System.IO.StreamWriter
You use a StreamWriter object to write data to a text file.
There are a number of ways you can obtain the StreamWriter object
System.IO.StreamWriter oStreamWriter;
oStreamWriter = new System.IO.StreamWriter("C:\Temp\textfile.txt");
Using a FileStream
System.IO.StreamWriter oStreamWriter;
System.IO.FileStream oFileStream;
oFileStream = new System.IO.FileStream("C:\Temp\textfile.txt", FileMode.Open);
oStreamWriter = new System.IO.StreamWriter(objFileStream);
Using a Stream
This lets you specify the mode, access and share
System.IO.StreamWriter oStreamWriter;
System.IO.Stream oStream;
oStream = System.IO.File.Open("C:\Temp\textfile.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
oStreamWriter = new StreamWriter(objStream);
Using File.OpenWrite
System.IO.StreamWriter oStreamWriter;
System.IO.FileStream oFileStream;
oFileStream = System.IO.File.OpenWrite("C:\Temp\textfile.txt");
oStreamWriter = new System.IO.StreamWriter(objFileStream);
File.CreateText
System.IO.StreamWriter oStreamWriter;
oStreamWriter = System.IO.File.CreateText("C:\myfile.txt");
oStreamWriter.Write("this is some text");
oStreamWrite.Close;
There are several ways to write to a StreamWriter
Write a Single Line
This only works with strings and appends a newline character to the end.
string sText;
oStreamWriter.WriteLine(sText);
BinaryReader
System.IO.BinaryReader
Binary files 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.
BinaryWriter
System.IO.BinaryWriter
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev