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}\t{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

 

posted @ 2019-07-17 17:27  特洛伊-Micro  阅读(786)  评论(0编辑  收藏  举报