C# - Path Class And Special Folders

Path Class And Special Folders

The static Path class defines methods and fields for working with paths and filenames . Assuming this setup code:

string dir = @"c:\mydir";
string file = @"myfile.txt";
string path = @"c:\mydir\myfile.txt";
Directory.SetCurrentDirectory (@"c:\demo");

we can demonstrate Path’s methods and fields with the following expressions

Expression: Result
Directory.GetCurrentDirectory() c:\demo\
Path.IsPathRooted (file) False
Path.IsPathRooted (path) True
Path.GetPathRoot (path) c:\
Path.GetDirectoryName (path) c:\mydir
Path.GetFileName (path) myfile.txt
Path.GetFullPath (file) c:\demo\myfile.txt
Path.Combine (dir, file) c:\mydir\myfile.txt
File extensions: Result
Path.HasExtension (file) True
Path.GetExtension (file) .txt
Path.GetFileNameWithoutExtension (file) myfile
Path.ChangeExtension (file, ".log") myfile.log
Temporary files:
Path.GetTempPath() \Temp
Path.GetRandomFileName() d2dwuzjf.dnp
Path.GetTempFileName() \Temp\tmp14B.tmp

Special Folders

One thing missing from Path and Directory is a means to locate folders such as My Documents, Program Files, Application Data, and so on. This is provided instead by the GetFolderPath method in the System.Environment class:

string myDocPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

Environment.SpecialFolder is an enum whose values encompass all special directories in Windows:

AdminTools CommonVideos Personal
ApplicationData Cookies PrinterShortcuts
CDBurning Desktop ProgramFiles
CommonAdminTools DesktopDirectory ProgramFilesX86
CommonApplicationData Favorites Programs
CommonDesktopDirectory Fonts Recent
CommonDocuments History Resources
CommonMusic InternetCache SendTo
CommonOemLinks LocalApplicationData StartMenu
CommonPictures LocalizedResources Startup
CommonProgramFiles MyComputer System
CommonProgramFilesX86 MyDocuments SystemX86
CommonPrograms MyMusic Templates
CommonStartMenu MyPictures UserProfile
CommonStartup MyVideos Windows
CommonTemplates NetworkShortcuts

The following method returns the .NET Framework directory:

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

Querying Volume Information

You can query the drives on a computer with the DriveInfo class:

DriveInfo c = new DriveInfo ("C"); // Query the C: drive.
long totalSize = c.TotalSize; // Size in bytes.
long freeBytes = c.TotalFreeSpace; // Ignores disk quotas.
long freeToMe = c.AvailableFreeSpace; // Takes quotas into account.
foreach (DriveInfo d in DriveInfo.GetDrives()) // All defined drives.
{
Console.WriteLine (d.Name); // C:\
Console.WriteLine (d.DriveType); // Fixed
Console.WriteLine (d.RootDirectory); // C:\
if (d.IsReady) // If the drive is not ready, the following two
// properties will throw exceptions:
{
Console.WriteLine (d.VolumeLabel); // The Sea Drive
Console.WriteLine (d.DriveFormat); // NTFS
}
}

The static GetDrives method returns all mapped drives, including CD-ROMs, media cards, and network connections. DriveType is an enum with the following values:

Unknown, NoRootDirectory, Removable, Fixed, Network, CDRom, Ram