C# Windows file system operations

C# File Input and Output

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 NumberAttributes & Description
1Attributes
Get the attributes of the current file or directory.
2CreationTime
Get the creation time of the current file or directory.
3Exists
Gets a boolean value indicating whether the directory exists.
4Extension
Gets a string indicating whether the file exists.
5FullName
Get the full path of the directory or file.
6LastAccessTime
Gets the time when the current file or directory was last accessed.
7Name
Gets the name of this DirectoryInfo instance.

The table below listsDirectoryInfoSome commonly used members in the classMethods:

Serial NumberMethods & Description
1public void Create()
Create a directory.
2public DirectoryInfo CreateSubdirectory( string path )
Creates a subdirectory on the specified path. The specified path can be relative to the instance of the DirectoryInfo class.
3public override void Delete()
If it is empty, delete the DirectoryInfo.
4public DirectoryInfo[] GetDirectories()
Return the subdirectories of the current directory.
5public 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 NumberAttributes & Description
1Attributes
Gets the attributes of the current file.
2CreationTime
Gets the creation time of the current file.
3Directory
Get an instance of the directory to which the file belongs.
4Exists
Gets a boolean value indicating whether the file exists.
5Extension
Gets a string indicating whether the file exists.
6FullName
Gets the full path of the file.
7LastAccessTime
Gets the time the current file was last accessed.
8LastWriteTime
Gets the time when the file was last written to.
9Length
Gets the size of the current file, in bytes.
10Name
Gets the name of the file.

The table below listsFileInfoSome commonly used members in the classMethods:

Serial NumberMethods & Description
1public StreamWriter AppendText()
Creates a StreamWriter that appends text to the file represented by the FileInfo instance.
2public FileStream Create()
Create a file.
3public override void Delete()
Permanently deletes a file.
4public void MoveTo( string destFileName )
Moves a specified file to a new location, providing options to specify a new file name.
5public FileStream Open( FileMode mode )
Opens a file in the specified mode.
6public FileStream Open( FileMode mode, FileAccess access )
Opens a file with the specified mode, using read, write, or read/write access.
7public 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.
8public FileStream OpenRead()
Creates a read-only FileStream.
9public 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;
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.

C# File Input and Output

other extensions