C# allows you to manipulate directories and files using classes related to various directories and files, such as The following table lists some Serial number Attribute & description 1 Attributes gets the properties 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 that represents the existence of the file. 5 FullName gets the full path to a directory or file. 6 LastAccessTime gets when the current file or directory was last accessed. 7 Name gets the name of the DirectoryInfo instance. The following table lists some Serial number Method and description 1 Public void Create () creates a directory. 2 Public DirectoryInfo CreateSubdirectory (string path) creates a subdirectoryon the specified path. The specified path can be a path relative to an instance of the DirectoryInfo class. 3 Public override void Delete () deletes the DirectoryInfo if it is empty. 4 Public DirectoryInfo [] GetDirectories () returns a subdirectory of the current directory. 5 Public FileInfo [] GetFiles () returns a list of files from the current directory. For a complete list of properties and methods, please visit Microsoft’s C # documentation. The following table lists some Serial number Attribute & description 1 Attributes gets the properties of the current file. 2 CreationTime gets the creation time of the current file. 3 Directory gets 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 that represents the existence of the file. 6 FullName gets the full path to the file. 7 LastAccessTime gets when the current file was last accessed. 8 LastWriteTime gets the time when the file was last written. 9 Length gets the size of the current file, in bytes. 10 Name gets the name of the file. The following table lists some Serial number Method and description 1 Public StreamWriter AppendText () creates a StreamWriter and appends text tothe file represented by an 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 usingread, write, or read/write access in the specified mode. 7 Public FileStream Open (FileMode mode, FileAccess access, FileShare share) opens a file in the specified mode, using read, write, or read/write access,as well as specified sharing options. 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 Microsoft’s C # documentation. The following example demonstrates the use of the classes mentioned above: When you compile and execute the above program, it displays the names of thefiles and their size in the Windows directory. DirectoryInfo class and FileInfo class. 1.46.1. DirectoryInfo class #
DirectoryInfo class is derived from the FileSystemInfo . It provides various methods for creating, moving and browsing directories and subdirectories. This class cannot be inherited. DirectoryInfo commonly used properties inthe class: DirectoryInfo commonly used methods: 1.46.2. FileInfo class #
FileInfo class is derived from the FileSystemInfo . It provides properties and methods for creating, copying, deleting, moving andopening files, and helps FileStream with creation of objects. This class cannot be inherited. FileInfo commonly used properties: FileInfo commonly used methods: 1.46.3. 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"); // Obtain 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(); } } }