如何读取扩展文件属性/文件元数据

所以,我按照教程使用ASP.net核心“上传”文件到本地路径,这是代码:

public IActionResult About(IList files) { foreach (var file in files) { var filename = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); filename = hostingEnv.WebRootPath + $@"\{filename}"; using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } return View(); } 

我想读取文件的扩展属性(文件元数据),如:

  • 名称,
  • 作者,
  • 发布日期,
  • 等等

并使用此数据对文件进行排序,是否有使用Iformfile的方法?

如果你想访问更多文件元数据,那么.NET框架提供了ootb,我想你需要使用第三方库。 否则,您需要编写自己的COM包装器来访问这些详细信息。

请参阅此链接以获取纯C#示例。

这里是一个如何读取文件属性的示例:

将Shell32.dll的引用从“Windows / System32”文件夹添加到项目中

 List arrHeaders = new List(); List> attributes = new List>(); Shell32.Shell shell = new Shell32.Shell(); var strFileName = @"C:\Users\Admin\Google Drive\image.jpg"; Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName)); Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName)); for (int i = 0; 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, 20); for (int i = 0; i < arrHeaders.Count; i++) { var attrName = arrHeaders[i]; var attrValue = objFolder.GetDetailsOf(folderItem, i); var attrIdx = i; attributes.Add(new Tuple(attrIdx, attrName, attrValue)); Debug.WriteLine("{0}\t{1}: {2}", i, attrName, attrValue); } Console.ReadLine(); 

您可以丰富此代码以创建自定义类,然后根据您的需要进行排序。

那里有许多付费版​​本,但有一个名为WindowsApiCodePack的免费版本

例如,访问图像元数据,我认为它支持

 ShellObject picture = ShellObject.FromParsingName(file); var camera = picture.Properties.GetProperty(SystemProperties.System.Photo.CameraModel); newItem.CameraModel = GetValue(camera, String.Empty, String.Empty); var company = picture.Properties.GetProperty(SystemProperties.System.Photo.CameraManufacturer); newItem.CameraMaker = GetValue(company, String.Empty, String.Empty);