c# - 我在使用 c# 读取文件属性“文件类型”时遇到问题
问题描述
我正在尝试使用 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;
}
解决方案
你试过FileVersionInfo.FileVersion
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
"Version number: " + myFileVersionInfo.FileVersion);
推荐阅读
- javascript - css 文件未为 ejs 文件中的特定路由加载
- javascript - ReactJS ES6 箭头函数
- r - 在 R 中计算正确的特征向量以找到特征分解
- django - Postgres 不尊重 Django 在 models.py 中的“on_delete = models.CASCADE”。为什么?
- haskell-stack - 为什么 Haskell Stack 一直在下载相同的东西?
- javascript - Javascript:如何使用reduce函数和concat数组函数将数组数组转换为数组?
- java - 由于不希望的匹配类型,无法覆盖子类中的方法
- javascript - 如何修复javascript中未定义的div?
- php - 在定制器上获取 wordpress 错误“changeset_post_save_failure”
- excel - 如何从函数返回布尔值?