C# Windows file system operations
C# allows you to use various directory and file related classes to manipulate directories and files, such asDirectoryInfoclass andFileInfoclass.
DirectoryInfo class
DirectoryInfoClass derived fromFileSystemInfoclass. It provides various methods for creating, moving, and enumerating directories and subdirectories. This class cannot be inherited.
The table below listsDirectoryInfoSome commonly used members in the classProperty:
| Serial Number | Attributes & Description |
|---|---|
| 1 | Attributes Get the attributes of the current file or directory. |
| 2 | CreationTime Get the creation time of the current file or directory. |
| 3 | Exists Gets a boolean value indicating whether the directory exists. |
| 4 | Extension Gets a string indicating whether the file exists. |
| 5 | FullName Get the full path of the directory or file. |
| 6 | LastAccessTime Gets the time when the current file or directory was last accessed. |
| 7 | Name Gets the name of this DirectoryInfo instance. |
The table below listsDirectoryInfoSome commonly used members in the classMethods:
| Serial Number | Methods & Description |
|---|---|
| 1 | public void Create() Create a directory. |
| 2 | public DirectoryInfo CreateSubdirectory(
string path
)
Creates a subdirectory on the specified path. The specified path can be relative to the instance of the DirectoryInfo class. |
| 3 | public override void Delete() If it is empty, delete the DirectoryInfo. |
| 4 | public DirectoryInfo[] GetDirectories() Return the subdirectories of the current directory. |
| 5 | public FileInfo[] GetFiles() Return the list of files from the current directory. |
For the full list of properties and methods, visit Microsoft's C# documentation.
FileInfo class
FileInfoClass derived fromFileSystemInfoclass. It provides properties and methods for creating, copying, deleting, moving, and opening files, and facilitates the creation of FileStream objects. This class cannot be inherited.
The table below listsFileInfoSome commonly used members in the classProperty:
| Serial Number | Attributes & Description |
|---|---|
| 1 | Attributes Gets the attributes of the current file. |
| 2 | CreationTime Gets the creation time of the current file. |
| 3 | Directory Get an instance of the directory to which the file belongs. |
| 4 | Exists Gets a boolean value indicating whether the file exists. |
| 5 | Extension Gets a string indicating whether the file exists. |
| 6 | FullName Gets the full path of the file. |
| 7 | LastAccessTime Gets the time the current file was last accessed. |
| 8 | LastWriteTime Gets the time when the file was last written to. |
| 9 | Length Gets the size of the current file, in bytes. |
| 10 | Name Gets the name of the file. |
The table below listsFileInfoSome commonly used members in the classMethods:
| Serial Number | Methods & Description |
|---|---|
| 1 | public StreamWriter AppendText() Creates a StreamWriter that appends text to the file represented by the FileInfo instance. |
| 2 | public FileStream Create() Create a file. |
| 3 | public override void Delete() Permanently deletes a file. |
| 4 | public void MoveTo(
string destFileName
)
Moves a specified file to a new location, providing options to specify a new file name. |
| 5 | public FileStream Open(
FileMode mode
)
Opens a file in the specified mode. |
| 6 | public FileStream Open(
FileMode mode,
FileAccess access
)
Opens a file with the specified mode, using read, write, or read/write access. |
| 7 | public FileStream Open(
FileMode mode,
FileAccess access,
FileShare share
)
Opens a file with the specified mode, using read, write, or read/write access, and the specified sharing options. |
| 8 | public FileStream OpenRead() Creates a read-only FileStream. |
| 9 | public FileStream OpenWrite() Creates a write-only FileStream. |
For the full list of properties and methods, visit Microsoft's C# documentation.
Example
The following example demonstrates the use of the classes mentioned above:
Example
using System.IO;
namespace WindowsFileApplication
{
class Program
{
static void Main(string[] args)
{
// Create a DirectoryInfo object
DirectoryInfo mydir = new DirectoryInfo(@"c:\Windows");
// Get the files in the directory as well as their names and sizes
FileInfo [] f = mydir.GetFiles();
foreach (FileInfo file in f)
{
Console.WriteLine("File Name: {0} Size: {1}",
file.Name, file.Length);
}
Console.ReadKey();
}
}
}
When you compile and execute the above program, it displays the names of the files and their sizes in the Windows directory.
other extensions