C# 获取文件扩展信息-应用名称/作者等
2024-09-02 05:34:45
方案一:使用微乳封装的Shell包
添加nuget包:Microsoft.WindowsAPICodePack.Shell
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
1 string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "需求文档_迭代版_036.docx");
var file = ShellFile.FromFilePath(filePath); //Read and Write: string[] oldAuthors = file.Properties.System.Author.Value;
string oldTitle = file.Properties.System.Title.Value;
var orgAppName = file.Properties.System.ApplicationName; file.Properties.System.Author.Value = new string[] { "Author #1", "Author #2" };
file.Properties.System.Title.Value = "Example Title"; // Alternate way to Write: ShellPropertyWriter propertyWriter = file.Properties.GetPropertyWriter();
propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { "Author" });
propertyWriter.Close();
2 直接使用Shell32交互
添加Com组件引用:
List<string> arrHeaders = new List<string>();
List<Tuple<int, string, string>> attributes = new List<Tuple<int, string, string>>(); Shell32.Shell shell = new Shell32.Shell();
var strFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "需求文档_迭代版_036.docx"); Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName)); for (int i = ; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
} // The attributes list below will contain a tuple with attribute index, name and value
// Once you know the index of the attribute you want to get,
// you can get it directly without looping, like this:
var Authors = objFolder.GetDetailsOf(folderItem, ); for (int i = ; i < arrHeaders.Count; i++)
{
var attrName = arrHeaders[i];
var attrValue = objFolder.GetDetailsOf(folderItem, i);
var attrIdx = i; attributes.Add(new Tuple<int, string, string>(attrIdx, attrName, attrValue)); Debug.WriteLine("{0}\t{1}: {2}", i, attrName, attrValue);
}
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
Windows API Code Pack 1.1.zip
https://github.com/jamie-pate/KeepSync/blob/master/contrib/Windows%20API%20Code%20Pack%201.1.zip
最新文章
- java中transient关键字的作用
- Symbol not found for architecture arm64 错误
- 回车与换行 ---编码等相关讨论----由notepad++ 批量替换 引发的讨论,转义字符也是人为硬性规定的。
- 【tips】判断两个整数是否是同一个数量级
- ClassNotFoundException异常的解决方法
- redis提示Could not get a resource from the pool(jedis连接池配置)
- [改善Java代码]构造代码块会想你所想
- StorSimple 简介
- 如何断开eclipse与svn的链接
- Scala-的元组和映射
- centos防火墙端口配置
- PHP SPL他们留下的宝石
- php用魔术方法__call实现类函数重载
- MVC TO LINQ
- php数组合并,反转,去重,添加元素等;
- nginx简单安装设置
- C#反射详解
- 使用Swagger 搭建高可读性ASP.Net WebApi文档
- JS中的跨域问题
- MySQL查询实例