Looping through the files

Using the DirectoryInfo class, we can also loop through the files present in the directory. The following code shows how we can loop through the files and access their properties:

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

In the preceding code snippet, we are looping through the files present in the Chapter 20 directory and showing the information present in it. In the Chapter 20 folder, we have only one file: dynamics365eula.txt. Thus, when the program is executed, it will pick up the file and will read the file information present in it. To illustrate this, we are displaying the filename, the time the file was created, and the time the file was last accessed. So when the code is executed, we will get the following output:

Now that we have some knowledge about drives and DirectoryInfo, we will explore some helper classes that allow us to do operations on files.

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

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