如何获取实际(本地化)文件夹名称?

How to get the actual (localized) folder names?(如何获取实际(本地化)文件夹名称?)

本文介绍了如何获取实际(本地化)文件夹名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 c# 编写一个功能,我需要列出给定目录中的所有文件/文件夹名称.该功能在 EN 操作系统上运行良好,但是当我在本地化操作系统(例如)德语上运行应用程序时,我仍然得到特殊文件夹的英文名称(程序文件而不是程序,收藏夹而不是收藏夹等).我不认为 Environment.GetFolderPath 和 Environment.SpecialFolder 可以有任何帮助,因为它与我想要的完全相反,即它给出了枚举的特殊文件夹的完整路径,而我想要给定的本地化名称小路.我曾尝试使用 File、SHFileInfo,但没有用.任何想法,我怎样才能获得操作系统中显示的文件夹名称?

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

这篇关于如何获取实际(本地化)文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何获取实际(本地化)文件夹名称?

基础教程推荐