.net - 如何在 .NET 中获取文件扩展名的描述

标签 .net windows

你好,

Windows 提供文件扩展名的说明,例如 .cpl 文件的“控制面板项目”和 .daa 文件的“PowerISO 文件”。有什么办法可以在 .NET 中获取这些数据吗?我使用 C#,但可以阅读所有其他 .NET 语言。还有没有办法获得扩展的默认图标?任何帮助将不胜感激。

提前致谢

最佳答案

您可以使用 SHGetFileInfo获取该信息的 API。这是一个包装器方法:

    public static string GetFileTypeDescription(string fileNameOrExtension)
    {
        SHFILEINFO shfi;
        if (IntPtr.Zero != SHGetFileInfo(
                            fileNameOrExtension,
                            FILE_ATTRIBUTE_NORMAL,
                            out shfi,
                            (uint)Marshal.SizeOf(typeof(SHFILEINFO)),
                            SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME))
        {
            return shfi.szTypeName;
        }
        return null;
    }

    [DllImport("shell32")]
    private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint flags);

    [StructLayout(LayoutKind.Sequential)]
    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;
    }

    private const uint FILE_ATTRIBUTE_READONLY = 0x00000001;
    private const uint FILE_ATTRIBUTE_HIDDEN = 0x00000002;
    private const uint FILE_ATTRIBUTE_SYSTEM = 0x00000004;
    private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
    private const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
    private const uint FILE_ATTRIBUTE_DEVICE = 0x00000040;
    private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
    private const uint FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
    private const uint FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
    private const uint FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;
    private const uint FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
    private const uint FILE_ATTRIBUTE_OFFLINE = 0x00001000;
    private const uint FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
    private const uint FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
    private const uint FILE_ATTRIBUTE_VIRTUAL = 0x00010000;

    private const uint SHGFI_ICON = 0x000000100;     // get icon
    private const uint SHGFI_DISPLAYNAME = 0x000000200;     // get display name
    private const uint SHGFI_TYPENAME = 0x000000400;     // get type name
    private const uint SHGFI_ATTRIBUTES = 0x000000800;     // get attributes
    private const uint SHGFI_ICONLOCATION = 0x000001000;     // get icon location
    private const uint SHGFI_EXETYPE = 0x000002000;     // return exe type
    private const uint SHGFI_SYSICONINDEX = 0x000004000;     // get system icon index
    private const uint SHGFI_LINKOVERLAY = 0x000008000;     // put a link overlay on icon
    private const uint SHGFI_SELECTED = 0x000010000;     // show icon in selected state
    private const uint SHGFI_ATTR_SPECIFIED = 0x000020000;     // get only specified attributes
    private const uint SHGFI_LARGEICON = 0x000000000;     // get large icon
    private const uint SHGFI_SMALLICON = 0x000000001;     // get small icon
    private const uint SHGFI_OPENICON = 0x000000002;     // get open icon
    private const uint SHGFI_SHELLICONSIZE = 0x000000004;     // get shell size icon
    private const uint SHGFI_PIDL = 0x000000008;     // pszPath is a pidl
    private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;     // use passed dwFileAttribute

(大多数常量实际上并没有在该代码中使用,但我还是把它们放在了一起,以防您想根据您的特定需求调整代码)

关于.net - 如何在 .NET 中获取文件扩展名的描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3780028/

相关文章:

.net - 为什么 .NET GUID 中有破折号?

c# - .NET:值类型继承 - 技术限制?

PHP 正则表达式使 Apache 崩溃

windows - 如何为动画 GIF 加水印并将 2 个 ImageMagick 命令组合在一起?

.net - Get-Date 转换为字符串 vs ToString()

c# - 如何重构这些方法以避免重复?

windows - gnuplot:如何获取包含非 ANSI 字符的文件列表?

node.js - Gulp安装: 'fs: re-evaluating native module sources is not supported' and node-sass errors

c# - 获取具有给定返回类型的所有方法

java - Windows - 在哪里找到 .bashrc 文件以增加堆大小