【问题标题】:Getting ANY extended file attribute in C#在 C# 中获取任何扩展文件属性
【发布时间】:2015-03-28 06:33:46
【问题描述】:

我正在尝试找到一种方法来获取 C# 中 COM DLL 上的扩展文件属性(特别是“产品版本”)。我在 MSDN 上通过添加“Microsoft Shell 控件和自动化”COM 参考找到了一些关于使用 Shell32 的示例,但文档似乎有些含糊。有没有简单的方法来做到这一点?

例如:取C:\Windows\Notepad.exe的如下属性:

我想以编程方式获取 C# 中的“产品版本”属性。顺便说一句,这可以是任何文件,但是,我只是使用 Notepad.exe,因为它是一个通用示例

【问题讨论】:

    标签: c#


    【解决方案1】:

    或者,您可以使用FileVersionInfo 类在一行中执行此操作:

    Console.WriteLine(FileVersionInfo.GetVersionInfo(@"C:\Windows\notepad.exe").ProductVersion);
    

    【讨论】:

    • 好答案,事实上,对于我正在尝试做的事情,这可能是比我更好的解决方案,但是,如果您需要获取其他属性,我认为这还不够。
    • @Ryan,FileVersionInfo 类可能具有您需要获得的所有属性。去看看吧。 msdn.microsoft.com/en-us/library/…
    • @vcsjones 顺便说一句,我忘了感谢您发布此答案。这就是我真正需要的,没有为 Shell32 的东西添加 COM 引用的额外复杂性。我同意 FileVersionInfo 有很多属性,但是,在某些情况下(即,如果我想从 mp3 中获取“流派”属性)我需要的不仅仅是 FileVersionInfo。
    • @ryan,是的,如果您需要这些属性,Win32 是最好/唯一的方法。
    【解决方案2】:

    我想出了以下易于使用的函数,它将返回任何文件属性的值:

     public static string GetExtendedFileAttribute(string filePath, string propertyName)
        {
            string retValue = null;
            Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
            object shell = Activator.CreateInstance(shellAppType);
            Shell32.Folder folder = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { @"C:\Windows\System32" });
            int? foundIdx = null;
            for (int i = 0; i < short.MaxValue; i++)
            {
                string header = folder.GetDetailsOf(null, i);
                if (header == propertyName)
                {
                    foundIdx = i;
                    break;
                }
            }
    
            if (foundIdx.HasValue)
            {
                foreach (FolderItem2 item in folder.Items())
                {
                    if (item.Name.ToUpper() == System.IO.Path.GetFileName(filePath).ToUpper())
                    {
                        retValue = folder.GetDetailsOf(item, foundIdx.GetValueOrDefault());
                        break;
                    }
                }
            }
    
    
            return retValue;
    
        }
    

    这是一个如何调用的示例:

     static void Main(string[] args)
        {
            Console.WriteLine(GetExtendedFileAttribute(@"C:\Windows\Notepad.exe", "Product version"));   
            Console.ReadLine();
        }
    

    这是输出:

    【讨论】:

    • “FolderItem2”从何而来?
    【解决方案3】:

    .NET 具有执行此操作的内置方法。

    MessageBox.Show("Notepad product version " + GetProductVersion("C:\\Windows\\notepad.exe"), "Product Version");
    
    public string GetProductVersion(string fileName)
    {
        System.Diagnostics.FileVersionInfo fileVersionInfo =
            System.Diagnostics.FileVersionInfo.GetVersionInfo(fileName);
        return fileVersionInfo.ProductVersion;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-13
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 2011-07-15
      • 2010-09-18
      相关资源
      最近更新 更多