【问题标题】:How to get the file type name?如何获取文件类型名称?
【发布时间】:2012-03-24 22:43:43
【问题描述】:

我想在我的 c# 应用程序中获取文件类型的名称,它显示在 Windows 的文件属性中...例如 .log 文件的类型为 LOG file (.log).bat 具有 Batch file for Windows (.bat) (翻译自我的语言,所以可能不准确)。

请问,我在哪里可以找到这些信息?或如何做到这一点? 我找到了Get-Registered-File-Types-and-Their-Associated-Ico 文章,其中作者显示了如何获取图标,但没有显示操作系统中显示的文件类型名称。

【问题讨论】:

  • 见链接stackoverflow.com/questions/770023/… - 你需要FriendlyDocName
  • 请理解,任何扩展都不会拥有此信息。使用该应用程序的第一个应用程序必须在注册表中注册此信息。

标签: c# file properties


【解决方案1】:

您可以从注册表中读取该信息

使用 GetDescription("cpp")GetDescription(".xml")

public static string ReadDefaultValue(string regKey)
{
    using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(regKey, false))
    {
        if (key != null)
        {
            return key.GetValue("") as string;
        }
    }
    return null;
}

public static string GetDescription(string ext)
{
    if (ext.StartsWith(".") && ext.Length > 1) ext = ext.Substring(1);

    var retVal = ReadDefaultValue(ext + "file");
    if (!String.IsNullOrEmpty(retVal)) return retVal;


    using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("." + ext, false))
    {
        if (key == null) return "";

        using (var subkey = key.OpenSubKey("OpenWithProgids"))
        {
            if (subkey == null) return "";

            var names = subkey.GetValueNames();
            if (names == null || names.Length == 0) return "";

            foreach (var name in names)
            {
                retVal = ReadDefaultValue(name);
                if (!String.IsNullOrEmpty(retVal)) return retVal;
            }
        }
    }

    return "";
}

【讨论】:

    【解决方案2】:

    您必须调用相应的 Shell 函数 SHGetFileInfo,这是一个原生 Win32 API。

    class NativeMethods
    {
        private const int FILE_ATTRIBUTE_NORMAL = 0x80;
        private const int SHGFI_TYPENAME = 0x400;
    
        [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
        private static extern IntPtr SHGetFileInfo(
            string pszPath,
            int dwFileAttributes,
            ref  SHFILEINFO shinfo,
            uint cbfileInfo,
            int uFlags);
    
    
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct SHFILEINFO
        {
            public SHFILEINFO(bool b)
            {
                hIcon = IntPtr.Zero;
                iIcon = 0;
                dwAttributes = 0;
                szDisplayName = "";
                szTypeName = "";
            }
    
            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;
        };
    
    
        public static string GetShellFileType(string fileName)
        {
            var shinfo = new SHFILEINFO(true);
            const int flags = SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES;
    
            if (SHGetFileInfo(fileName, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags) == IntPtr.Zero)
            {
                return "File";
            }
    
            return shinfo.szTypeName;
        }
    }
    

    然后,只需致电NativeMethods.GetShellFileType("...")

    【讨论】:

      【解决方案3】:

      你必须使用 shgetfileinfo,一些代码见下面的链接:

      http://www.pinvoke.net/default.aspx/shell32.shgetfileinfo

      【讨论】:

      • 这仅显示如何检索图标,而不是文件类型名称。虽然额外的工作量很小,但如果您实际展示要做什么,而不是仅仅发布链接,将会很有帮助。
      【解决方案4】:

      您可以使用 SHGetFileInfo 获取此信息。

      using System;
      using System.Runtime.InteropServices;
      
      namespace GetFileTypeAndDescription
      {
      
      class Class1
      {
      [STAThread]
      static void Main(string[] args)
      {
      SHFILEINFO shinfo = new SHFILEINFO();
      IntPtr i = Win32.SHGetFileInfo(@"d:\temp\test.xls", 0, ref
      shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_TY PENAME);
      string s = Convert.ToString(shinfo.szTypeName.Trim());
      Console.WriteLine(s);
      }
      }
      
      [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;
      };
      
      class Win32
      {
      public const uint SHGFI_DISPLAYNAME = 0x00000200;
      public const uint SHGFI_TYPENAME = 0x400;
      public const uint SHGFI_ICON = 0x100;
      public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
      public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
      
      [DllImport("shell32.dll")]
      public static extern IntPtr SHGetFileInfo(string pszPath, uint
      dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-18
        • 2019-09-01
        • 2011-02-04
        • 2013-07-27
        • 1970-01-01
        • 2018-11-12
        • 2013-11-29
        相关资源
        最近更新 更多