How to read extended file properties / file metadata

后端 未结 1 443

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

public IActionResult About(IList files)
         


        
相关标签:
1条回答
  • 2020-12-07 00:17

    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:\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);
    
    0 讨论(0)
提交回复
热议问题