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 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);
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext