C# Windows File System Operations
C# allows you to manipulate directories and files using various classes related to directories and files, such as the DirectoryInfo class and the FileInfo class.
DirectoryInfo Class
The DirectoryInfo class is derived from the FileSystemInfo class. It provides methods for creating, moving, browsing directories and subdirectories. This class cannot be inherited.
The following table lists some commonly used properties of the DirectoryInfo class:
Number | Property & Description |
---|---|
1 | Attributes <br>Gets the attributes of the current file or directory. |
2 | CreationTime <br>Gets the creation time of the current file or directory. |
3 | Exists <br>Gets a Boolean value indicating whether the directory exists. |
4 | Extension <br>Gets a string representing the file extension. |
5 | FullName <br>Gets the full path of the directory or file. |
6 | LastAccessTime <br>Gets the time the current file or directory was last accessed. |
7 | Name <br>Gets the name of this DirectoryInfo instance. |
The following table lists some commonly used methods of the DirectoryInfo class:
Number | Method & Description |
---|---|
1 | public void Create() <br>Creates a directory. |
2 | public DirectoryInfo CreateSubdirectory(<br> string path<br>) <br>Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class. |
3 | public override void Delete() <br>Deletes the DirectoryInfo if it is empty. |
4 | public DirectoryInfo[] GetDirectories() <br>Returns the subdirectories of the current directory. |
5 | public FileInfo[] GetFiles() <br>Returns a file list from the current directory. |
For a complete list of properties and methods, please visit the Microsoft C# documentation.
FileInfo Class
The FileInfo class is derived from the FileSystemInfo class. It provides properties and methods for creating, copying, deleting, moving, and opening files, and is helpful for creating FileStream objects. This class cannot be inherited.
The following table lists some commonly used properties of the FileInfo class:
Number | Property & Description |
---|---|
1 | Attributes <br>Gets the attributes of the current file. |
2 | CreationTime <br>Gets the creation time of the current file. |
3 | Directory <br>Gets an instance of the directory to which the file belongs. |
4 | Exists <br>Gets a Boolean value indicating whether the file exists. |
5 | Extension <br>Gets a string representing the file extension. |
6 | FullName <br>Gets the full path of the file. |
7 | LastAccessTime <br>Gets the time the current file was last accessed. |
8 | LastWriteTime <br>Gets the time the current file was last written to. |
9 | Length <br>Gets the size of the current file in bytes. |
10 | Name <br>Gets the name of the file. |
The following table lists some commonly used methods of the FileInfo class:
Number | Method & Description |
---|---|
1 | public StreamWriter AppendText() <br>Creates a StreamWriter that appends text to the file represented by the instance of the FileInfo. |
2 | public FileStream Create() <br>Creates a file. |
3 | public override void Delete() <br>Permanently deletes a file. |
4 | public void MoveTo(<br> string destFileName<br>) <br>Moves a specified file to a new location, providing the option to specify a new file name. |
5 | public FileStream Open(<br> FileMode mode<br>) <br>Opens a file in the specified mode. |
6 | public FileStream Open(<br> FileMode mode,<br> FileAccess access<br>) <br>Opens a file with the specified mode, using read, write, or read/write access. |
7 | public FileStream Open(<br> FileMode mode,<br> FileAccess access,<br> FileShare share<br>) <br>Opens a file with the specified mode, using read, write, or read/write access, and the specified sharing option. |
8 | public FileStream OpenRead() <br>Creates a read-only FileStream. |
9 | public FileStream OpenWrite() <br>Creates a write-only FileStream. |
To view the complete list of properties and methods, please visit Microsoft's C# documentation.
Example
The following example demonstrates the usage of the mentioned class:
using System;
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 and 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 run the above program, it displays the names and sizes of the files in the Windows directory.