views:

75

answers:

3

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

+2  A: 

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);

}
John Weldon
thanks John, sorry i didn't get my edit about SHGetFolderPath in before you started responding! I'm wondering if there is a ".NET Way" to do it
Brien W.
I'm assuming it's in the registry somewhere, but I don't know for sure. That may be the route you want to explore if your willing to take the risk of accessing undocumented info.
John Weldon
sounds like i'm better off just using the shell32 way.
Brien W.
A: 

Try casting 0x19 and 0x16 to Environment.SpecialFolder to pass to Environment.GetFolderPath

Joel Lucsy
throws an exception: "Illegal enum value"
Brien W.
+2  A: 

Hi, 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

keyboardP