I did figure out how to get this to work. Not sure what's wrong with the above code (I also got Chinese Unicode characters), but this seems to work reliably. Just pass in the path (for example, by calling:
GetDisplayName(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
and it returns the display name of the folder (in this example, "My Documents" or whatever you've renamed it to).
using System.Runtime.InteropServices;
...
public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
public const uint SHGFI_DISPLAYNAME = 0x000000200; // get display name
[DllImport("shell32")]
public static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes,
out SHFILEINFO psfi, uint cbFileInfo, uint flags);
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
public static string GetDisplayName(string path)
{
SHFILEINFO shfi = new SHFILEINFO();
if (0 != SHGetFileInfo(path,FILE_ATTRIBUTE_NORMAL,out shfi,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),SHGFI_DISPLAYNAME))
{
return shfi.szDisplayName;
}
return null;
}