In C#, we can get the path of a special folder, such as Desktop, Program Files, Programs, Start Menu and Startup using the .net function Environment.GetFolderPath. and we can also get All User’s common profile path by using the API (“shell32.dll”) function SHGetSpecialFolderPath.
Get System Special Folder Path
Use the below C# code to retrieve the system special folder path.
string programFilesPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); Console.WriteLine(programFilesPath); string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System); Console.WriteLine(systemPath);
Get Current User Special Folder Path
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Returns current user's desktop profile path like: C:UsersMorganDesktop Console.WriteLine(desktopPath); string startMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu); // Ex: C:UsersMorganAppDataRoamingMicrosoftWindowsStart Menu Console.WriteLine(startMenuPath);
Get All Users Profile Path (Common Special Folder Path)
You can get common profile path for All Users by using API function SHGetSpecialFolderPath. The below C# method gets common desktop profile path and all users start menu programs folder path.
[DllImport("shell32.dll")] static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate); const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x19; const int CSIDL_COMMON_STARTMENU = 0x16; public static void GetCommonProfilePath() { StringBuilder allUserProfile = new StringBuilder(260); SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_DESKTOPDIRECTORY, false); string commonDesktopPath = allUserProfile.ToString(); //The above API call returns: C:UsersPublicDesktop Console.WriteLine(commonDesktopPath); SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_STARTMENU, false); //The above API call returns: C:ProgramDataMicrosoftWindowsStart Menu string startMenuPrograms = Path.Combine(allUserProfile.ToString(), "Programs"); Console.WriteLine(startMenuPrograms); }
The API function SHGetSpecialFolderPath also supports to get the path of following special folders:
const int CSIDL_APPDATA = 0x1a; const int CSIDL_COMMON_APPDATA = 0x23; const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x19; const int CSIDL_COMMON_DOCUMENTS = 0x2e; const int CSIDL_COMMON_MUSIC = 0x35; const int CSIDL_COMMON_OEM_LINKS = 0x3a; const int CSIDL_COMMON_PICTURES = 0x36; const int CSIDL_COMMON_PROGRAMS = 0x17; const int CSIDL_COMMON_STARTMENU = 0x16; const int CSIDL_COMMON_STARTUP = 0x18; const int CSIDL_COMMON_TEMPLATES = 0x2d; const int CSIDL_COMMON_VIDEO = 0x37; const int CSIDL_COOKIES = 0x21; const int CSIDL_DESKTOP = 0; const int CSIDL_DESKTOPDIRECTORY = 0x10; const int CSIDL_DRIVES = 0x11; const int CSIDL_FAVORITES = 6; const int CSIDL_FLAG_CREATE = 0x8000; const int CSIDL_FLAG_DONT_VERIFY = 0x4000; const int CSIDL_FONTS = 20; const int CSIDL_HISTORY = 0x22; const int CSIDL_INTERNET_CACHE = 0x20; const int CSIDL_LOCAL_APPDATA = 0x1c; const int CSIDL_MYMUSIC = 13; const int CSIDL_MYPICTURES = 0x27; const int CSIDL_MYVIDEO = 14; const int CSIDL_PERSONAL = 5; const int CSIDL_PROGRAM_FILES = 0x26; const int CSIDL_PROGRAM_FILES_COMMON = 0x2b; const int CSIDL_PROGRAM_FILES_COMMONX86 = 0x2c; const int CSIDL_PROGRAM_FILESX86 = 0x2a; const int CSIDL_PROGRAMS = 2; const int CSIDL_RECENT = 8; const int CSIDL_SENDTO = 9; const int CSIDL_STARTMENU = 11; const int CSIDL_STARTUP = 7; const int CSIDL_SYSTEM = 0x25; const int CSIDL_SYSTEMX86 = 0x29; const int CSIDL_TEMPLATES = 0x15; const int CSIDL_WINDOWS = 0x24;
Advertisement