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

AppendAllLinesOpens a file and appends lines to the end of the file and closes the file.
AppendAllLinesAsyncAsychronously opens a file and appends lines to the end of the file and closes the file.
AppendAllTextOpens a file and appends a string to the end of the file and then closes the file.
AppendAllTextAsyncAsychronously opens a file and appends a string to the end of the file and then closes the file.
AppendTextOpens a text file in append mode returning a StreamWriter object
CopyCopies a file to a new destination
CreateCreates and opens a file returning a FileStream object
CreateTextCreates a text file returning a StreamWriter object
DeleteDeletes a file
ExistsReturns True if the file exists
GetAttributesReturns the attributes of a file
GetCreationTimeReturns the creation time of the file
GetLastAccessTimeReturns the last access time of the file
GetLastWriteTimeReturns the last write time of a file
MoveMoves a file
OpenOpens a file with a specified mode, access and share returning a FileStream object
OpenReadOpens a file in read mode returning a FileStream object
OpenTextOpens a text file for reading returning a StreamReader object
OpenWriteOpens a file in write mode returning a FileStream object
ReadAllBytesOpens a binary file and reads the contents into a byte array and then closes the file.
ReadAllBytesAsyncAschronously opens a binary file and reads the contents into a byte array and then closes the file.
ReadAllLinesOpens a text file and reads all the lines of the file and then closes the file.
ReadAllLinesAsyncAsynchronously opens a text file and reads all the lines of the file and then closes the file.
ReadAllTextOpens a text file and reads all the text in the file and then closes the file.
ReadAllTextAsyncAsynchronously opens a text file and reads all the text in the file and closes the file.
ReadLinesReads all the lines of a file.
ReplaceReplaces the contents os a specified file with the contents of another file, deleting the original file and creating a backup of the replaced file.
SetAttributesSets the attributes for a file
SetCreatiionTimeSets the creation time of the file
SetLastAccessTimeSets the last access time of the file
SetLastWriteTimeSets the last write time of the file


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.


GetCreationTime

Returns a DateTime telling you when the file was created.

System.IO.File.GetCreationTime(path); 

SetCreatiionTime


System.IO.File.SetCreatiionTime(path, datetime); 

GetLastAccessTime


System.IO.File.GetLastAccessTime(path); 

SetLastAccessTime


System.IO.File.SetLastAccessTime(path, datetime); 

GetLastWriteTime


System.IO.File.GetLastWriteTime(path); 

SetLastWriteTime


System.IO.File.SetLastWriteTime(path, datetime); 

Exists


Will determine if a file currently exists.

System.IO.File.Exists(path); 

Create

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]); 

CreateText

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); 

Delete

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);
}

Open

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

OpenRead

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");

OpenWrite

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); 

OpenText

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); 

Copy


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);

AppendText

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); 

Move

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) 

Attributes


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) 


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext