如何读取扩展的文件属性/文件元数据 [英] How to read extended file properties / file metadata

查看:289
本文介绍了如何读取扩展的文件属性/文件元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我遵循了一个教程,使用ASP.net核心将文件上传"到本地路径, 这是代码:

So, i followed a tutorial to "upload" files to a local path using ASP.net core, this is the code:

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();
    }

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

I want to read the extended properties of a file (file metadata)like:

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

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

and to sort the files using this data, is there a way using Iformfile?

推荐答案

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

If you want to access more file metadata then the .NET framework provides ootb, I guess you need to use a third party library. Otherwise you need to write your own COM wrapper to access those details.

有关纯C#示例,请参见此链接.

下面是一个如何读取文件属性的示例:

将对Shell32.dll的引用从"Windows/System32"文件夹添加到 您的项目

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();

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

You can enrich this code to create custom classes and then do sorting depending on your needs.

有很多付费版本,但有一个免费版本,称为

There are many paid versions out there, but there is a free one called WindowsApiCodePack

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

For example accessing image metadata, I think it supports

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);

这篇关于如何读取扩展的文件属性/文件元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆