如何获取本地化的特殊文件夹名称?

时间:2012-10-31 05:12:54

标签: c# winapi shell localization globalization

我使用SHGetFileInfo()GetDisplayNameOf()来获取特殊文件夹的名称。

如果本地化的操作系统要更改“非Unicode程序的当前语言”设置,这些函数将返回一个值“??? ?????????”。

用户遇到的这种设置组合。

shell32是不是完全兼容unicode?

Shell32.STRRET STRRET;
STRRET.uType = (uint)Shell32.STRRET_TYPE.STRRET_WSTR;
if (Windows.S_OK != ishellfolder_parent.GetDisplayNameOf(ptr_pidllast, (uint)Shell32.SHGNO.SHGDN_NORMAL | (uint)Shell32.SHGNO.SHGDN_INFOLDER, out STRRET))
                return null;

StringBuilder sbuilder = new StringBuilder(260);
Shell32.StrRetToBuf(ref STRRET, ptr_pidllast, sbuilder, (uint)sbuilder.Capacity);

出了什么问题?

***后来补充道 另一个展示我问题的例子:

public static partial class Program
{
    const Int32 CSIDL_DESKTOP = (0x0000);
    const uint SHGFI_DISPLAYNAME = 0x000000200;     // get display name
    const uint SHGFI_PIDL = 0x000000008;     // pszPath is a pidl

    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
        public static int NAMESIZE = 80;
        public IntPtr hIcon;
        public int iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };

    [DllImport("shell32.dll")]
    static extern IntPtr SHGetFileInfo(IntPtr pidl, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
    [DllImport("shell32.dll")]
    public static extern IntPtr SHCloneSpecialIDList(IntPtr hwnd, Int32 CSIDL, bool create);


    [STAThread]
    static void Main(string[] args)
    {
        IntPtr pidl = SHCloneSpecialIDList(IntPtr.Zero, CSIDL_DESKTOP, false);
        SHFILEINFO shfi = new SHFILEINFO();
        if (IntPtr.Zero != SHGetFileInfo(
                    pidl,
                    0,
                    ref shfi,
                    (uint)Marshal.SizeOf(typeof(SHFILEINFO)),
                    SHGFI_PIDL | SHGFI_DISPLAYNAME))
        {
            System.Windows.Forms.MessageBox.Show(shfi.szDisplayName);
        }

此代码不正确。有些情况下错误的返回值,如上所述。 任何人都可以帮我提供正确代码的示例,与Unicode完全兼容并使用非默认系统设置吗?


谢谢大家!经过一番实验,找到了解决方案。我的错误在这里:

Shell32.StrRetToBuf(ref STRRET, ptr_pidllast, sbuilder, (uint)sbuilder.Capacity);

签名应为:

[DllImport("shlwapi.dll", CharSet=CharSet.Unicode, EntryPoint="StrRetToBufW")]
public static extern Int32 StrRetToBufW( ...

1 个答案:

答案 0 :(得分:1)

有人问了同样的问题。您可以在那里使用代码示例。

How to get the actual (localized) folder names?