Csharp Windows File System
[ C# File Input and Output](#)
C# allows you to use various directory and file-related classes to manipulate directories and files, such as the **DirectoryInfo** class and the **FileInfo** class.
The **DirectoryInfo** class is derived from the **FileSystemInfo** class. It provides various methods for creating, moving, and browsing directories and subdirectories. This class cannot be inherited.
The following table lists some common **properties** of the **DirectoryInfo** class:
| No. | Property & Description |
| --- | --- |
| 1 | **Attributes** Gets the attributes of the current file or directory. |
| 2 | **CreationTime** Gets 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 representing the file extension. |
| 5 | **FullName** Gets the full path of the directory or file. |
| 6 | **LastAccessTime** Gets the time the current file or directory was last accessed. |
| 7 | **Name** Gets the name of this DirectoryInfo instance. |
The following table lists some common **methods** of the **DirectoryInfo** class:
| No. | Method & Description |
| --- | --- |
| 1 | **public void Create()** Creates a directory. |
| 2 | **public DirectoryInfo CreateSubdirectory( string path )** Creates a subdirectory on a specified path. The specified path can be relative to the instance of the DirectoryInfo class. |
| 3 | **public override void Delete()** Deletes this DirectoryInfo if it is empty. |
| 4 | **public DirectoryInfo[] GetDirectories()** Returns the subdirectories of the current directory. |
| 5 | **public FileInfo[] GetFiles()** Returns a file list from the current directory. |
For a complete list of properties and methods, please visit the Microsoft C# documentation.
The **FileInfo** class is derived from the **FileSystemInfo** class. It provides properties and methods for creating, copying, deleting, moving, and opening files, and it assists in the creation of FileStream objects. This class cannot be inherited.
The following table lists some common **properties** of the **FileInfo** class:
| No. | Property & Description |
| --- | --- |
| 1 | **Attributes** Gets the attributes of the current file. |
| 2 | **CreationTime** Gets the creation time of the current file. |
| 3 | **Directory** Gets an instance of the directory in which the file resides. |
| 4 | **Exists** Gets a boolean value indicating whether the file exists. |
| 5 | **Extension** Gets a string representing the file extension. |
| 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 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 following table lists some common **methods** of the **FileInfo** class:
| No. | Method & Description |
| --- | --- |
| 1 | **public StreamWriter AppendText()** Creates a StreamWriter that appends text to the file represented by this instance of FileInfo. |
| 2 | **public FileStream Create()** Creates 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 in the specified mode with read, write, or read/write access. |
| 7 | **public FileStream Open( FileMode mode, FileAccess access, FileShare share )** Opens a file in the specified mode with read, write, or read/write access and the specified sharing option. |
| 8 | **public FileStream OpenRead()** Creates a read-only FileStream. |
| 9 | **public FileStream OpenWrite()** Creates a write-only FileStream. |
For a complete list of properties and methods, please visit the Microsoft C# documentation.
The following example demonstrates the usage of the classes mentioned above:
## Example
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 along with 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 will display the names of the files and their sizes in the Windows directory.
[ C# File Input and Output](#)
YouTip