The FileSystemInfo class contains methods that are common to file and directory manipulation. A FileSystemInfo object can represent either a file or a directory, thus serving as the basis for FileInfo or DirectoryInfo objects. Use this base class when parsing a lot of files and directories.

A derived class can inherit from FileSystemInfo only if the derived class has the AllAccess permission from the FileIOPermissionAccess enumeration.

In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

using System;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
staticvoid Main(string[] args)
{
// Loop through all the immediate subdirectories of C.foreach (string entry in Directory.GetDirectories(@"C:\"))
{
DisplayFileSystemInfoAttributes(new DirectoryInfo(entry));
}
// Loop through all the files in C.foreach (string entry in Directory.GetFiles(@"C:\"))
{
DisplayFileSystemInfoAttributes(new FileInfo(entry));
}
}
staticvoid DisplayFileSystemInfoAttributes(FileSystemInfo fsi)
{
// Assume that this entry is a file.string entryType = "File";
// Determine if entry is really a directoryif ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory )
{
entryType = "Directory";
}
// Show this entry's type, name, and creation date.
Console.WriteLine("{0} entry {1} was created on {2:D}", entryType, fsi.FullName, fsi.CreationTime);
}
}
}
// Output will vary based on contents of drive C.// Directory entry C:\Documents and Settings was created on Tuesday, November 25, 2003// Directory entry C:\Inetpub was created on Monday, January 12, 2004// Directory entry C:\Program Files was created on Tuesday, November 25, 2003// Directory entry C:\RECYCLER was created on Tuesday, November 25, 2003// Directory entry C:\System Volume Information was created on Tuesday, November 2, 2003// Directory entry C:\WINDOWS was created on Tuesday, November 25, 2003// File entry C:\IO.SYS was created on Tuesday, November 25, 2003// File entry C:\MSDOS.SYS was created on Tuesday, November 25, 2003// File entry C:\pagefile.sys was created on Saturday, December 27, 2003