A Look at C# Directory and DirectoryInfo Classes - Modifying the Example
(Page 2 of 4 )
Copy the following code and paste it in to replace the previous code example:
using System;
using System.IO;
namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Please enter the name of the folder you want to create or use");
// store the folder name in the dirName variable
string dirName = Console.ReadLine();
// create the DirectoryInfo instance
DirectoryInfo dInfo = new DirectoryInfo(@"F:" + dirName);
// check if the directory doesn't exist, if so then
// create it
if(!dInfo.Exists)
{
dInfo.Create();
}
Console.WriteLine("How many subfolders you want to create?");
// store the number of subfolders in the variable
// subDirs
int subDirs = Convert.ToInt32(Console.ReadLine());
// create the subfolders using a for statement
for(int i = 1; i <= subDirs; i++)
{
dInfo.CreateSubdirectory(dirName + i);
}
Console.WriteLine("The following Subfolders have been" +
" created in the folder " + dirName);
// get the subfolders using the
// DirectoryInfo.GetDirectories() method
DirectoryInfo[] dirs = dInfo.GetDirectories();
// printing the first subfolder information
DirectoryInfo firstSDir = dirs[0];
Console.WriteLine(firstSDir.FullName);
Console.WriteLine(firstSDir.Name);
Console.WriteLine(firstSDir.Root);
Console.WriteLine(firstSDir.CreationTime);
Console.WriteLine(firstSDir.LastAccessTime);
Console.WriteLine(firstSDir.Attributes);
firstSDir.Attributes |= FileAttributes.Archive;
Console.WriteLine(firstSDir.Attributes);
Console.WriteLine("The Folder Extension : " + firstSDir.Extension);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
Run the code and enter the same values as the previous example.

Because the MyFolder directory already exists, the if statement evaluates to false and the next statement (after the if statement) is executed. We entered the value 4 as before, so the DirectoryInfo.CreateSubDirectories will not create any subfolders (because the subfolders are already there). The difference is that we have replaced the foreach statement with the following code:
DirectoryInfo firstSDir = dirs[0];
Console.WriteLine(firstSDir.FullName);
Console.WriteLine(firstSDir.Name);
Console.WriteLine(firstSDir.Root);
Console.WriteLine(firstSDir.CreationTime);
Console.WriteLine(firstSDir.LastAccessTime);
Console.WriteLine(firstSDir.Attributes);
firstSDir.Attributes |= FileAttributes.Archive;
Console.WriteLine(firstSDir.Attributes);
Console.WriteLine("The Folder Extension : " + firstSDir.Extension);
The above code prints some useful information about the first subfolder in the MyFolder directory. This information includes the Name property, which returns the name of the folder; the Root property which returns the root; and the CreationTime and lastAccessTime properties are self explanatory.
The Attribute property is used to set or get some useful attributes of the file. Note that you use the | operator to combine those attributes. When we first print the value of the Attributes property we find that there is only one attribute set (Directory). We add another attribute using the FileAttributes Enumeration and print the value of Attributes property again. The Extension property returns an empty string when called on DirectoryInfo instances. The Extension property is inherited, as are much of the methods and properties of this class, from the base class FileSystemInfo. To check that the Extension property returns an empty string you can use the following code:
DirectoryInfo di = new DirectoryInfo(@"F:MyFolder");
if(di.Extension == String.Empty)
{
Console.WriteLine("Yes Empty String");
}
Next: Using the Directory Class >>
More C# Articles
More By Michael Youssef