• C# 获取文件扩展信息-应用名称/作者等


    方案一:使用微乳封装的Shell包

    添加nuget包:Microsoft.WindowsAPICodePack.Shell

    
    
    using Microsoft.WindowsAPICodePack.Shell;
    using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

    1
    string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "需求文档_迭代版_036.docx"); 2 var file = ShellFile.FromFilePath(filePath); 3 4 //Read and Write: 5 6 string[] oldAuthors = file.Properties.System.Author.Value; 7 string oldTitle = file.Properties.System.Title.Value; 8 var orgAppName = file.Properties.System.ApplicationName; 9 10 file.Properties.System.Author.Value = new string[] { "Author #1", "Author #2" }; 11 file.Properties.System.Title.Value = "Example Title"; 12 13 // Alternate way to Write: 14 15 ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter(); 16 propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { "Author" }); 17 propertyWriter.Close();

    2  直接使用Shell32交互

    添加Com组件引用:

     1             List<string> arrHeaders = new List<string>();
     2             List<Tuple<int, string, string>> attributes = new List<Tuple<int, string, string>>();
     3 
     4             Shell32.Shell shell = new Shell32.Shell();
     5             var strFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "需求文档_迭代版_036.docx");
     6 
     7             Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
     8             Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));
     9 
    10 
    11             for (int i = 0; i < short.MaxValue; i++)
    12             {
    13                 string header = objFolder.GetDetailsOf(null, i);
    14                 if (String.IsNullOrEmpty(header))
    15                     break;
    16                 arrHeaders.Add(header);
    17             }
    18 
    19             // The attributes list below will contain a tuple with attribute index, name and value
    20             // Once you know the index of the attribute you want to get, 
    21             // you can get it directly without looping, like this:
    22             var Authors = objFolder.GetDetailsOf(folderItem, 20);
    23 
    24             for (int i = 0; i < arrHeaders.Count; i++)
    25             {
    26                 var attrName = arrHeaders[i];
    27                 var attrValue = objFolder.GetDetailsOf(folderItem, i);
    28                 var attrIdx = i;
    29 
    30                 attributes.Add(new Tuple<int, string, string>(attrIdx, attrName, attrValue));
    31 
    32                 Debug.WriteLine("{0}	{1}: {2}", i, attrName, attrValue);
    33             }
    34             Console.ReadLine();

     参考资料:

    https://stackoverflow.com/questions/37869388/how-to-read-extended-file-properties-file-metadata

    https://stackoverflow.com/questions/220097/read-write-extended-file-properties-c/2096315#2096315

    https://stackoverflow.com/questions/24081665/windows-api-code-pack-where-is-it

    https://www.codeproject.com/Articles/5036/ID3-Tag-Reader-Using-Shell-Functions

    https://github.com/jamie-pate/KeepSync/blob/master/contrib/Windows%20API%20Code%20Pack%201.1.zip

  • 相关阅读:
    git修改文件名大小写的方法。
    VC中常用的宏
    spring cloud Zuul 多层拦截 --- 心得
    Zuul网关 @EnableZuulProxy 和 @EnableZuulServer 的区别
    jave 数据类型 float 的 正确赋值
    spring cloud bus 消息总线 动态刷新配置文件 【actuator 与 RabbitMQ配合完成】
    RabbitMQ 消息中间件 的下载与安装【window10】
    spring cloud --- 使用 actuator 热更新【刷新】单机配置文件
    spring cloud 与spring boot的版本对应总结
    spring cloud feign 报错 feign.FeignException$MethodNotAllowed: status 405 reading 解决
  • 原文地址:https://www.cnblogs.com/micro-chen/p/11202408.html
Copyright © 2020-2023  润新知