Drives and directories

A drive represents a storage medium for the filesystem. It can be a hard drive, a CD, or any other storage type. In .NET Framework, we have a DriveInfo class in the System.IO namespace, which helps us access the filesystem information that is available on the drive. It provides methods that can help us access information such as name, size, and the free space available on the drive. Please refer to the following code implementation, in which we are looping through all the files available on the drive:

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive Name" + d.Name);
Console.WriteLine(" Drive type " + d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine("Available space ", d.AvailableFreeSpace);
Console.WriteLine("Total size in bytes ", d.TotalSize);
}
}
Console.ReadLine();

In the preceding piece of code, we are browsing through all the drives (that is, C, D, E, and so on) available on the filesystem and are publishing information related to the following: 

  • The name of the drive
  • The type of drive, that is, fixed, RAM, CD ROM, removable, and so on
  • The total available memory size on the drive
  • The total free memory available on the drive

If we execute the code, the execution will loop through all the drives that are present in the filesystem. Once a drive is retrieved, the execution will retrieve certain properties about the drive, such as free space, total size, and drive type. Thus, when we execute the program, we will get the following output. Please note that we may not get all the information as it also depends upon the security permissions on the directory:

In the system in which we are executing this program, we just have a C drive. Thus, while the program is executing, we are showing the properties of the C drive. 

There are other properties on the driveinfo object as well. If we click on Go to Definition on the DriveInfo class, we can see the attributes of the class. Please visit the following link for more information: https://docs.microsoft.com/en-us/dotnet/api/system.io.driveinfo?view=netframework-4.7.2.

Each drive in a filesystem comprises directories and files. A directory in itself can comprise multiple sub-directories and files. If we need to do an operation on a particular directory, we do it using the DirectoryInfo class in C#. In the following code snippet, we are creating an object of the DirectoryInfo class, passing the location of a particular directory path:

DirectoryInfo directoryInfo = new DirectoryInfo("C:\UCN Code Base\Programming-in-C-Exam-70-483-MCSD-Guide\Book70483Samples");
foreach (DirectoryInfo f in directoryInfo.GetDirectories())
{
Console.WriteLine("Directory Name is" + f.Name);
Console.WriteLine("Directory created time is " + f.CreationTime);
Console.WriteLine("Directory last modified time is " + f.LastAccessTime);
}

For the directoryInfo object, we are then looping through all the child directories and are showing the information related to the following:

  • Name of the directory
  • Time the directory was created
  • Time the directory was last modified

When we execute the preceding program, we do so on the C:\UCN Code Base\Programming-in-C-Exam-70-483-MCSD-Guide\Book70483Samples file path. Please note that this is where we have been placing the codebase for the chapters we've got through in this book. Thus, when we execute this program, it will loop through the sub-folders of all those chapters and will fetch information such as Directory Name, Directory created time, and Directory last modified time. The following is the output that we will get for the program:

There are additional operations available with the DirectoryInfo object. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.70