| The DirectoryInfo class in C# represents a directory on your computer's file system. It provides methods for interacting with the directory, such as creating new directories, deleting existing ones, checking if a directory exists, listing the files and subdirectories within it, getting its attributes (like read-only status or hidden status), and obtaining its full path and name. FileInfo class provides a way to interact with individual files on your computer. It allows you to access file properties like name, size, creation date, last access time, and more. You can also use it to perform actions like reading file contents, writing to files, deleting files, and moving or copying files. FileInfo offers a convenient and structured approach to manage file-related operations within your C# applications.
Following is the simple C# example to fins file in given directory. using System.IO; // added for FileInfo class
.
.
.
.
DirectoryInfo dir = new DirectoryInfo(@"F:\WINNT");
FileInfo[] bmpfiles = dir.GetFiles("*.bmp");
Console.WriteLine("Total number of bmp files", bmpfiles.Length);
foreach( FileInfo f in bmpfiles)
{
Console.WriteLine("Name is : {0}", f.Name);
Console.WriteLine("Length of the file is : {0}", f.Length);
Console.WriteLine("Creation time is : {0}", f.CreationTime);
Console.WriteLine("Attributes of the file are : {0}",
f.Attributes.ToString());
}
|