gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:06 27 4
gpt4 key购买 nike

因此,我按照教程使用 ASP.net Core 将文件“上传”到本地路径,这是代码:

public IActionResult About(IList<IFormFile> 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 包装器才能访问这些详细信息。

See this link for a pure C# sample.

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

Add Reference to Shell32.dll from the "Windows/System32" folder to your project

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 = @"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<int, string, string>(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);

关于c# - 如何读取扩展文件属性/文件元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37869388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com