Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“我在读取文件属性时遇到问题”;“文件类型”;用c#_C#_File Properties - Fatal编程技术网

“我在读取文件属性时遇到问题”;“文件类型”;用c#

“我在读取文件属性时遇到问题”;“文件类型”;用c#,c#,file-properties,C#,File Properties,我试图用c#提取值“file version”,但结果总是空的。所有其他值的读数似乎都正常。有人有什么建议吗 公共静态字符串GetExtendedFileProperty(字符串文件路径,字符串属性名称) { 字符串值=string.Empty; 字符串baseFolder=Path.GetDirectoryName(filePath); 字符串fileName=Path.GetFileName(filePath); //方法加载并执行Windows server 8环境的外壳对象,否则您将获

我试图用c#提取值“file version”,但结果总是空的。所有其他值的读数似乎都正常。有人有什么建议吗

公共静态字符串GetExtendedFileProperty(字符串文件路径,字符串属性名称)
{
字符串值=string.Empty;
字符串baseFolder=Path.GetDirectoryName(filePath);
字符串fileName=Path.GetFileName(filePath);
//方法加载并执行Windows server 8环境的外壳对象,否则您将获得“无法将'System.\u ComObject'类型的COM对象强制转换为接口类型'Shell32.Shell'”
类型shellAppType=Type.GetTypeFromProgID(“Shell.Application”);
Object shell=Activator.CreateInstance(shellAppType);
Shell32.Folder shellFolder=(Shell32.Folder)shellAppType.InvokeMember(“名称空间”,System.Reflection.BindingFlags.InvokeMethod,null,shell,新对象[]{baseFolder});
//Parsename将在Shell32.Folder对象中找到我要查找的特定文件
Shell32.FolderItem FolderItem=shellFolder.ParseName(文件名);
if(folderitem!=null)
{
对于(int i=0;i”+值);
}
}
//如果找不到指定属性的值,则返回string.Empty
返回值;
}
你试过了吗


尝试使用FileInfo类。“FileInfo FileInfo=newfileinfo(strFilename);'查看GetDetailsOf()函数的文档,我觉得它不能返回FileVersion。请尝试System.Diagnostics.FileVersionInfo类。请参阅:FileVersionInfo只能在可执行文件上工作,这不是一个。第二个论点是。为什么索引32对你不起作用很难猜测,你应该提到你得到了什么。
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 myFileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
           "Version number: " + myFileVersionInfo.FileVersion);