Sunday, 31 May 2009

How to get commonly used directories using .Net

Many times we require users home directory and other commonly used directories in .NET, this can be achieved using a combination of the Environment.SpecialFolders enumeration, and the Environment.GetFolderPath() function.

Special Folders

Environment.SpecialFolder enumeration contains:

Environment.SpecialFolder.ApplicationData
Environment.SpecialFolder.System
Environment.SpecialFolder.CommonApplicationData
Environment.SpecialFolder.CommonProgramFiles
Environment.SpecialFolder.Cookies
Environment.SpecialFolder.Desktop
Environment.SpecialFolder.DesktopDirectory
Environment.SpecialFolder.Favorites
Environment.SpecialFolder.History
Environment.SpecialFolder.InternetCache
Environment.SpecialFolder.LocalApplicationData
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyMusic
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Personal
Environment.SpecialFolder.ProgramFiles
Environment.SpecialFolder.Programs
Environment.SpecialFolder.Recent
Environment.SpecialFolder.SendTo
Environment.SpecialFolder.StartMenu

This enumeration can be used to get specific folder path, using
Environment.GetFolderPath as:

public static String GetMyDocumentsDir()
{
return Environment.GetFolderPath(Environment.SpecialFolder.Personal);
}

It's a nice simple trick for getting system dependent information while using .NET

Happy programming !

Please leave your comments if it helped you!!

No comments:

Post a Comment