.NET File System Class inside System.IO namespace

.NET File system classes separated into two informational and utility.

Information classes are derived from FileSystemInfo base class. They expose files, folders and drives and FileInfo and DirectoryInfo are such classes. In addition there is DriveInfo class which is not derived from FileSystemInfo since you cannot remove or delete drives.

Utility classes provide static methods and perform operations on files, folders and drives.  Examples of such classes are: File, Directory, and Path

FileInfo class deals with files by manipulating them.



Directory Gets the DirectoryInfo object
DirectoryName Gets the name of the directory
IsReadOnly Gets or sets the flag that file can be modified or deleted
Length Gets the length of the file



AppendText Creates a new StreamWriter.
CopyTo Makes a copy of the file.
Create Creates a file based on the current file info.
CreateText Creates a new StreamWriter.
Decrypt Decrypts a file that was encrypted by the current user.
Encrypt Encrypts a file so that only the current user can decrypt the information within the file.
MoveTo Moves the file to a new location.
Open Opens the file with specific privileges (read, read/write, and so on).
OpenRead Opens the file with read-only access.
OpenText Opens the file and returns a StreamReader to allow reading of text within the file.
OpenWrite Opens the file with write-only access.
Replace Replaces a file with the information in the current FileInfo object.

Working with FileInfo class in order to read it,
FileInfo ourFileObject = new FileInfo(@"c:\boot.ini ");
if (ourFile.Exists)
    Console.WriteLine("File : {0}", ourFileObject.Name); //Read Name
    ourFileObject.CopyTo(@"c:\bootfile.bak");//Copy File
    File.Open("file.txt", FileMode.Create, FileAccess.Write);


DirectoryInfo class is used to access and work with the directory structure.



Attributes Gets or sets the FileAttributes of the current FileSystemInfo.
CreationTime the creation time of the current FileSystemInfo.
CreationTimeUtc the creation time, in coordinated universal time (UTC), of the current FileSystemInfo.
Exists a value indicating whether the directory exists.
Extension the string representing the extension part of the file.
FullName the full path of the directory or file.
LastAccessTime the time the current file or directory was last accessed.
LastAccessTimeUtc the time, in coordinated universal time (UTC).
LastWriteTime the time when the current file or directory was last written to.
LastWriteTimeUtc the time, in coordinated universal time (UTC).
Name Overridden. Gets the name of this DirectoryInfo.
Parent the parent directory of a specified subdirectory.
Root the root portion of a path.



Create Creates the directory
CreateSubdirectory Creates a child directory
GetDirectories Gets an array of Directory objects
GetFiles Gets an array of File objects
GetFileSystemInfos Gets an array of File System objects
MoveTo Moves the current directory to a new location

Working with DirectoryInfo class
DirectoryInfo ourDirInfo = new DirectoryInfo(@"c:\windows");

Console.WriteLine("Dir: {0}", ourDirInfo.FullName);

foreach (FileInfo file in ourDirInfo.GetFiles())
    Console.WriteLine("File: {0}", file.Name);

DirveInfo class is used to manipulate single directory in the file system.



AvailableFreeSpace Determines the amount of available space
DriveFormat Determines the format of the drive
DriveType Determines the type of drive
IsReady Determines the status of the drive
Name Determines the name of the drive
RootDirectory Determines a DirectoryInfo object
TotalFreeSpace Determines the total amount of free space
TotalSize Determines the size of the drive
VolumeLabel Determines or sets the label of the drive



GetDrives Returns all the drives

DriveType Enumeration provides list of drives



CDRom Optical drive.
Fixed Fixed disk.
Network Network drive.
NoRootDirectory Drive without a root directory.
Ram RAM drive.
Removable Removable media dirve.
Unknown Could not be determined.

Working with DriveType and DriveInfo
DriveInfo[] ourDrives = DriveInfo.GetDrives();
foreach (DriveInfo ourDrive in ourDrives)
    Console.WriteLine("Drive: {0}", ourDrive.Name);
    Console.WriteLine("Type:  {0}", ourDrive.DriveType);

Path Class is used to manipulate file system path.



ChangeExtension Returns a new path with the file name's extension.
Combine Combines two path strings.
GetDirectoryName Gets the name of the directory.
GetExtension Gets the name of the file ext.
GetFileName Gets the name of the file.
GetFileNameWithoutExtension Gets the file name without the ext.
GetFullPath Gets a full path to the specified path.
GetPathRoot Gets the root dir.
GetRandomFileName Gets a random file name.
GetTempFileName Gets a temporary file.
GetTempPath Gets the path to the temp. file dir.
HasExtension Determines if path's file name has an extension.
IsPathRooted Determines if file is in a root directory.

Working with Path
string ourPath = @"c:\bootfile.ini";
Console.WriteLine("Extentions: {0}", Path.GetExtension(ourPath));
Console.WriteLine("Change Path: {0}", Path.ChangeExtension(ourPath, "bak")); //Changes a string of the path.

FileSystemWatcher Class monitors file system directory for change.



EnableRaisingEvents Enable event watcher object for events raise.
Filter Determines which file changes to monitor.
IncludeSubdirectories Indicates of whether the watching of a directory is to include subdirectories.
NotifyFilter Determines and Modifies the type of changes to watch.
Path Determines and Modifies the path to the dir.



WaitForChanged Watching a directory for changes



Changed Happens when directory has changed
Created Happens when a file or dir are created
Deleted Happens when a file or dir has been deleted
Renamed Occurs when a file or dir has been re-named

FileSystemWatcher watch = new FileSystemWatcher();
watch.Path = @"c:\";
// Register for events
watch.Created += new FileSystemEventHandler(watch_Changed);
watch.Deleted += new FileSystemEventHandler(watch_Changed);
// Start Watching
watch.EnableRaisingEvents = true;
// Event Handler
static void watch_Changed(object sender, FileSystemEventArgs e)
  Console.WriteLine("Dir changed({0}): {1}", e.ChangeType, e.FullPath);