Working with files & directories in c#


Working with Files & Directories in C#

C# provides the following classes to work with the File system. They can be used to access directories, access files, open files for reading or writing, create a new file or move existing files from one location to another, etc.
Class   Name  Usage
 FileThe file is a static class that provides different functionalities like copy, create, move, delete, open for reading or /writing, encrypt or decrypt, check if a file exists, append lines or text to a file’s content, get last access time, etc.
 FileInfoThe FileInfo class provides the same functionality as a static File class. You have more control on how you do read/write operations on a file by writing code manually for reading or writing bytes from a file.
DirectoryThe directory is a static class that provides functionality for creating, moving, deleting and accessing subdirectories.
DirectoryInfoDirectoryInfo provides instance methods for creating, moving, deleting and accessing subdirectories.
Path                                                                                                                  The path is a static class that provides functionality such as retrieving the extension of a file, changing the extension of a file, retrieving the absolute physical path, and other path related functionalities.

File

C# includes static File class to perform I/O operation on physical file system. The static File class includes various utility method to interact with physical file of any type e.g. binary, text etc.
Use this static File class to perform some quick operation on physical file. It is not recommended to use File class for multiple operations on multiple files at the same time due to performance reasons. Use FileInfo class in that scenario.

Important Methods of Static File Class

MethodUsage
AppendAllLinesAppends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.
AppendAllTextOpens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
AppendTextCreates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
CopyCopies an existing file to a new file. Overwriting a file of the same name is not allowed.
CreateCreates or overwrites a file in the specified path.
CreateTextCreates or opens a file for writing UTF-8 encoded text.
DecryptDecrypts a file that was encrypted by the current account using the Encrypt method.
DeleteDeletes the specified file.
EncryptEncrypts a file so that only the account used to encrypt the file can decrypt it.
ExistsDetermines whether the specified file exists.
GetAccessControlGets a FileSecurity object that encapsulates the access control list (ACL) entries for a specified file.
MoveMoves a specified file to a new location, providing the option to specify a new file name.
OpenOpens a FileStream on the specified path with read/write access.
ReadAllBytesOpens a binary file, reads the contents of the file into a byte array, and then closes the file.
ReadAllLinesOpens a text file, reads all lines of the file, and then closes the file.
ReadAllTextOpens a text file, reads all lines of the file, and then closes the file.
ReplaceReplaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file.
WriteAllBytesCreates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
WriteAllLinesCreates a new file, writes a collection of strings to the file, and then closes the file.
WriteAllText                                                              Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Post a Comment

2 Comments