如何获得实际的(本地化)文件夹名称? [英] How to get the actual (localized) folder names?
问题描述
我写在C#中的功能,在这里,我必须列出指定目录中的所有文件/文件夹名称。该功能运行在OS EN罚款,但是当我运行本地化操作系统的应用程序(例如)德国,我仍然得到特殊文件夹(Program Files文件,而不是计划,而不是最爱的法沃里滕等)的英文名称。我不认为Environment.GetFolderPath与Environment.SpecialFolder能有所帮助,因为它做的我想即它什么给所列举的特殊文件夹的完整路径正好相反,然而,我想给定的本地化名称路径。我一直在使用的文件,SHFILEINFO试过,但没有用的。任何想法,我怎么能获得文件夹名称显示在OS?
I am writing a functionality in c# where I am required to list all the files/folder names in a given directory. The functionality runs fine on EN OS, but when I run the application on localized OS (for e.g.) German, I am still getting the English names of the Special Folders(Program Files instead of Programme, Favourites instead of Favoriten etc.). I don't think that Environment.GetFolderPath with Environment.SpecialFolder can be of any help as it does the exactly opposite of what I want i.e it gives the Full Path of the Special Folder enumerated, whereas, I want the localized name of the given path. I have tried using File, SHFileInfo, but of no use. Any idea, how can i get the folder names as displayed in the OS?
推荐答案
你可以用的SHGetFileInfo
API的本地化显示名称:
You can get the localized display name with the SHGetFileInfo
API:
public static string GetDisplayName(Environment.SpecialFolder specialFolder)
{
IntPtr pidl = IntPtr.Zero;
try
{
HResult hr = SHGetFolderLocation(IntPtr.Zero, (int) specialFolder, IntPtr.Zero, 0, out pidl);
if (hr.IsFailure)
return null;
SHFILEINFO shfi;
if (0 != SHGetFileInfo(
pidl,
FILE_ATTRIBUTE_NORMAL,
out shfi,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
SHGFI_PIDL | SHGFI_DISPLAYNAME))
{
return shfi.szDisplayName;
}
return null;
}
finally
{
if (pidl != IntPtr.Zero)
ILFree(pidl);
}
}
public static string GetDisplayName(string path)
{
SHFILEINFO shfi;
if (0 != SHGetFileInfo(
path,
FILE_ATTRIBUTE_NORMAL,
out shfi,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
SHGFI_DISPLAYNAME))
{
return shfi.szDisplayName;
}
return null;
}
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
private const uint SHGFI_DISPLAYNAME = 0x000000200; // get display name
private const uint SHGFI_PIDL = 0x000000008; // pszPath is a pidl
[DllImport("shell32")]
private static extern int SHGetFileInfo(IntPtr pidl, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint flags);
[DllImport("shell32")]
private static extern HResult SHGetFolderLocation(IntPtr hwnd, int nFolder, IntPtr token, int dwReserved, out IntPtr pidl);
[DllImport("shell32")]
private static extern void ILFree(IntPtr pidl);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct SHFILEINFO
{
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;
}
[StructLayout(LayoutKind.Sequential)]
public struct HResult
{
private int _value;
public int Value
{
get { return _value; }
}
public Exception Exception
{
get { return Marshal.GetExceptionForHR(_value); }
}
public bool IsSuccess
{
get { return _value >= 0; }
}
public bool IsFailure
{
get { return _value < 0; }
}
}
这篇关于如何获得实际的(本地化)文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!