【问题标题】:I have problem reading file property "file type" with c#我在使用 c# 读取文件属性“文件类型”时遇到问题
【发布时间】:2018-11-14 13:21:01
【问题描述】:

我正在尝试使用 c# 提取值“文件版本”,但它一直为空。所有其他值似乎都可以读取。有人有什么建议吗?

public static string GetExtendedFileProperty(string filePath, string propertyName)
    {
        string value = string.Empty;
        string baseFolder = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);

        //Method to load and execute the Shell object for Windows server 8 environment otherwise you get "Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'"
        Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
        Object shell = Activator.CreateInstance(shellAppType);
        Shell32.Folder shellFolder = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { baseFolder });

        //Parsename will find the specific file I'm looking for in the Shell32.Folder object
        Shell32.FolderItem folderitem = shellFolder.ParseName(fileName);
        if (folderitem != null)
        {
            for (int i = 0; i < short.MaxValue; i++)
            {
                //Get the property name for property index i
                string property = shellFolder.GetDetailsOf(null, i);

                //Will be empty when all possible properties has been looped through, break out of loop
                if (String.IsNullOrEmpty(property)) break;


                //Skip to next property if this is not the specified property
                if (property != propertyName) continue;

                //Read value of property
                value = shellFolder.GetDetailsOf(folderitem, i);
                Console.WriteLine(property + " -> " + value);
            }
        }
        //returns string.Empty if no value was found for the specified property
        return value;
    }

【问题讨论】:

  • 尝试使用 FileInfo 类。 'FileInfo fileInfo = new FileInfo(strFilename);'
  • 看GetDetailsOf()函数的文档,感觉不能返回FileVersion。只需尝试 System.Diagnostics.FileVersionInfo 类。参考:docs.microsoft.com/en-us/dotnet/api/…
  • FileVersionInfo 只能处理可执行文件,这不是一个。第二个参数是documented here。为什么索引 32 对你不起作用很难猜,你应该提到你得到了什么。 Look here.

标签: c# file-properties


【解决方案1】:

你试过FileVersionInfo.FileVersion

FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
           "Version number: " + myFileVersionInfo.FileVersion);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2017-07-21
    • 2020-04-14
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    相关资源
    最近更新 更多