开发者

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


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


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

}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜