Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the path to the common Desktop and Start Menu dirs in C#?

I'm using .NET 2.0. I noticed that there doesn't seem to be a Environment.SpecialFolder member for the common Desktop and common Start Menu folders.

i would prefer a way that doesn't involve loading shell32.dll and using SHGetSpecialFolderPath

like image 886
Brien W. Avatar asked Jan 18 '10 23:01

Brien W.


2 Answers

This code snippet uses the registry to access the common desktop:

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine;
key = key.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
String commonDesktop = key.GetValue("Common Desktop").ToString();

From here

like image 113
keyboardP Avatar answered Oct 22 '22 04:10

keyboardP


I use P/Invoke... 0x19 corresponds to the Common Desktop enumeration, 0x16 corresponds to the Common Start Menu

    public static string GetCommonDesktopFolder()
    {
        var sb = new StringBuilder(260);
        SHGetFolderPath(IntPtr.Zero, 0x19, IntPtr.Zero, 0, sb); // CSIDL_COMMON_DESKTOPDIRECTORY
        return sb.ToString();
    }

    [DllImport("shell32.dll")]
    private static extern int SHGetFolderPath(
                IntPtr hwndOwner, int nFolder, IntPtr hToken,
                uint dwFlags, StringBuilder pszPath);

}
like image 35
John Weldon Avatar answered Oct 22 '22 05:10

John Weldon