读/写';扩展的';文件属性(C#)

读/写';扩展的';文件属性(C#),c#,extended-properties,C#,Extended Properties,我试图找出如何在C语言中读/写扩展文件属性# e、 g.在Windows资源管理器中可以看到的注释、比特率、访问日期、类别等。 有什么办法吗? 编辑:我将主要读/写视频文件(AVI/DIVX/…)有一个ID3阅读器。以及具有更多其他属性信息的。基本上,您需要为shell32.dll调用文件夹shell对象,我不确定您试图为哪些类型的文件编写属性,但它是一个优秀的开源标记库,可以很好地包装所有这些功能。它对大多数流行的媒体文件类型有很多内置支持,但也允许您对几乎任何文件进行更高级的标记 编辑:我已

我试图找出如何在C语言中读/写扩展文件属性# e、 g.在Windows资源管理器中可以看到的注释、比特率、访问日期、类别等。 有什么办法吗?
编辑:我将主要读/写视频文件(AVI/DIVX/…)

有一个ID3阅读器。以及具有更多其他属性信息的。基本上,您需要为
shell32.dll

调用文件夹shell对象,我不确定您试图为哪些类型的文件编写属性,但它是一个优秀的开源标记库,可以很好地包装所有这些功能。它对大多数流行的媒体文件类型有很多内置支持,但也允许您对几乎任何文件进行更高级的标记

编辑:我已经更新了指向taglib sharp的链接。旧的链接不再有效


编辑:根据kzu的评论再次更新链接。

VB.NET中的此示例读取所有扩展属性:

Sub Main()
        Dim arrHeaders(35)

        Dim shell As New Shell32.Shell
        Dim objFolder As Shell32.Folder

        objFolder = shell.NameSpace("C:\tmp")

        For i = 0 To 34
            arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i)
        Next
        For Each strFileName In objfolder.Items
            For i = 0 To 34
                Console.WriteLine(i & vbTab & arrHeaders(i) & ": " & objfolder.GetDetailsOf(strFileName, i))
            Next
        Next

    End Sub

您必须从“引用”对话框的“COM”选项卡添加对Microsoft Shell控件和自动化的引用。

对于那些对VB不感兴趣的人,这里是c#:

public static void Main(string[] args)
{
    List<string> arrHeaders = new List<string>();

    Shell32.Shell shell = new Shell32.Shell();
    Shell32.Folder objFolder;

    objFolder = shell.NameSpace(@"C:\temp\testprop");

    for( int i = 0; i < short.MaxValue; i++ )
    {
        string header = objFolder.GetDetailsOf(null, i);
        if (String.IsNullOrEmpty(header))
            break;
        arrHeaders.Add(header);
    }

    foreach(Shell32.FolderItem2 item in objFolder.Items())
    {
        for (int i = 0; i < arrHeaders.Count; i++)
        {
            Console.WriteLine(
              $"{i}\t{arrHeaders[i]}: {objFolder.GetDetailsOf(item, i)}");
        }
    }
}
注意,您必须从“引用”对话框的“COM”选项卡添加对Microsoft Shell控件和自动化的引用

public static void Main(string[] args)
{
    List<string> arrHeaders = new List<string>();

    Shell32.Shell shell = new Shell32.Shell();
    Shell32.Folder objFolder;

    objFolder = shell.NameSpace(@"C:\temp\testprop");

    for( int i = 0; i < short.MaxValue; i++ )
    {
        string header = objFolder.GetDetailsOf(null, i);
        if (String.IsNullOrEmpty(header))
            break;
        arrHeaders.Add(header);
    }

    foreach(Shell32.FolderItem2 item in objFolder.Items())
    {
        for (int i = 0; i < arrHeaders.Count; i++)
        {
            Console.WriteLine(
              $"{i}\t{arrHeaders[i]}: {objFolder.GetDetailsOf(item, i)}");
        }
    }
}
publicstaticvoidmain(字符串[]args)
{
List arrHeaders=新列表();
Shell=new Shell32.Shell();
Shell32.文件夹objFolder;
objFolder=shell.NameSpace(@“C:\temp\testprop”);
对于(int i=0;i
谢谢你们的帖子!当我想找出exe的文件版本时,它帮助了我。然而,我需要自己找出所谓扩展属性的最后一点

如果在Windows资源管理器中打开exe(或dll)文件的属性,则会显示“版本”选项卡以及该文件扩展属性的视图。我想访问其中一个值

解决此问题的方法是使用属性索引器FolderItem.ExtendedProperty,如果删除属性名称中的所有空格,则会得到该值。例如,文件版本变为文件版本,您就有了它

希望这对其他人有帮助,我只是想把这些信息添加到这个帖子中。干杯

GetDetailsOf()
方法-检索文件夹中项目的详细信息。例如,其大小、类型或上次修改的时间。文件属性可能因Windows操作系统的版本而异

List<string> arrHeaders = new List<string>();

 Shell shell = new ShellClass();
 Folder rFolder = shell.NameSpace(_rootPath);
 FolderItem rFiles = rFolder.ParseName(filename);

 for (int i = 0; i < short.MaxValue; i++)
 {
      string value = rFolder.GetDetailsOf(rFiles, i).Trim();
      arrHeaders.Add(value);
 }
List arrHeaders=新列表();
Shell=新的Shell类();
文件夹rFolder=shell.NameSpace(_rootPath);
FolderItem rFiles=rFolder.ParseName(文件名);
对于(int i=0;i
2016年解决方案 将以下NuGet包添加到项目中:

  • Microsoft.WindowsAPICodePack Shell
  • Microsoft.WindowsAPICodePack核心
读写属性 重要提示:

该文件必须是由指定的特定软件创建的有效文件。每种文件类型都有特定的扩展文件属性,并且并非所有文件都是可写的

若您在桌面上的文件上单击鼠标右键,但无法编辑属性,则也无法在代码中对其进行编辑

例如:

  • 在桌面上创建txt文件,将其扩展名重命名为docx。你不能 编辑其
    作者
    标题
    属性
  • 用Word打开它,编辑并保存 信息技术现在你可以了
因此,只要确保使用一些
尝试
捕获

进一步专题: 有点简单。下面是工作的示例代码:

对于那些无法静态引用shell32的用户,您可以如下所示动态调用它:

var shellAppType = Type.GetTypeFromProgID("Shell.Application");
dynamic shellApp = Activator.CreateInstance(shellAppType);
var folder = shellApp.NameSpace(folderPath);
foreach (var item in folder.Items())
{
    var company = item.ExtendedProperty("Company");
    var author = item.ExtendedProperty("Author");
    // Etc.
}
  • 在研究了该线程和其他线程上的许多解决方案之后 下面的代码放在一起。这仅用于读取属性
  • 我找不到电话 Shell32.FolderItem2.ExtendedProperty函数要工作,应该 获取字符串值并返回该字符串的正确值和类型 财产。。。这对我来说总是空的,开发人员参考资源非常少
  • 似乎 被微软抛弃,这给我们带来了下面的代码
使用:

  • 将返回所需的扩展属性的值作为 指定文件和属性名称的字符串
  • 仅循环,直到找到指定的属性-直到 所有属性都像一些示例代码一样被发现
  • 将在Windows版本(如Windows server 2008)上工作,如果只是尝试正常创建Shell32对象,则会出现错误

    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);
            }
        }
        //returns string.Empty if no value was found for the specified property
        return value;
    }
    
    公共静态字符串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将在中找到我要查找的特定文件
    
    string propertyValue = GetExtendedFileProperty("c:\\temp\\FileNameYouWant.ext","PropertyYouWant");
    
    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);
            }
        }
        //returns string.Empty if no value was found for the specified property
        return value;
    }
    
            var i = 0;
            while (true)
            {
                ...
                if (String.IsNullOrEmpty(header)) break;
                ...
                i++;
    
        private Dictionary<string, string> GetExtendedProperties(string filePath)
        {
            var directory = Path.GetDirectoryName(filePath);
            var shell = new Shell32.Shell();
            var shellFolder = shell.NameSpace(directory);
            var fileName = Path.GetFileName(filePath);
            var folderitem = shellFolder.ParseName(fileName);
            var dictionary = new Dictionary<string, string>();
            var i = -1;
            while (++i < 320)
            {
                var header = shellFolder.GetDetailsOf(null, i);
                if (String.IsNullOrEmpty(header)) continue;
                var value = shellFolder.GetDetailsOf(folderitem, i);
                if (!dictionary.ContainsKey(header)) dictionary.Add(header, value);
                Console.WriteLine(header +": " + value);
            }
            Marshal.ReleaseComObject(shell);
            Marshal.ReleaseComObject(shellFolder);
            return dictionary;
        }