Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

c# - How to read extended file properties / file metadata

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:

  • name,
  • author,
  • date posted,
  • etc

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

See this link for a pure C# sample.

Here an example how to read the properties of a file:

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:UsersAdminGoogle Driveimage.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}{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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...