Read/Write 'Extended' file properties (C#)

后端 未结 10 2204
我在风中等你
我在风中等你 2020-11-22 02:54

I\'m trying to find out how to read/write to the extended file properties in C# e.g. Comment, Bit Rate, Date Accessed, Category etc that you can see in Windows explorer. An

相关标签:
10条回答
  • 2020-11-22 03:32

    GetDetailsOf() Method - Retrieves details about an item in a folder. For example, its size, type, or the time of its last modification. File Properties may vary based on the Windows-OS version.

    List<string> arrHeaders = new List<string>();
    
     Shell shell = new ShellClass();
     Folder rFolder = shell.NameSpace(_rootPath);
     FolderItem rFiles = rFolder.ParseName(filename);
    
     for (int i = 0; i < short.MaxValue; i++)
     {
          string value = rFolder.GetDetailsOf(rFiles, i).Trim();
          arrHeaders.Add(value);
     }
    
    0 讨论(0)
  • 2020-11-22 03:36

    Here is a solution for reading - not writing - the extended properties based on what I found on this page and at help with shell32 objects.

    To be clear this is a hack. It looks like this code will still run on Windows 10 but will hit on some empty properties. Previous version of Windows should use:

            var i = 0;
            while (true)
            {
                ...
                if (String.IsNullOrEmpty(header)) break;
                ...
                i++;
    

    On Windows 10 we assume that there are about 320 properties to read and simply skip the empty entries:

        private Dictionary<string, string> GetExtendedProperties(string filePath)
        {
            var directory = Path.GetDirectoryName(filePath);
            var shell = new Shell32.Shell();
            var shellFolder = shell.NameSpace(directory);
            var fileName = Path.GetFileName(filePath);
            var folderitem = shellFolder.ParseName(fileName);
            var dictionary = new Dictionary<string, string>();
            var i = -1;
            while (++i < 320)
            {
                var header = shellFolder.GetDetailsOf(null, i);
                if (String.IsNullOrEmpty(header)) continue;
                var value = shellFolder.GetDetailsOf(folderitem, i);
                if (!dictionary.ContainsKey(header)) dictionary.Add(header, value);
                Console.WriteLine(header +": " + value);
            }
            Marshal.ReleaseComObject(shell);
            Marshal.ReleaseComObject(shellFolder);
            return dictionary;
        }
    

    As mentioned you need to reference the Com assembly Interop.Shell32.

    If you get an STA related exception, you will find the solution here:

    Exception when using Shell32 to get File extended properties

    I have no idea what those properties names would be like on a foreign system and couldn't find information about which localizable constants to use in order to access the dictionary. I also found that not all the properties from the Properties dialog were present in the dictionary returned.

    BTW this is terribly slow and - at least on Windows 10 - parsing dates in the string retrieved would be a challenge so using this seems to be a bad idea to start with.

    On Windows 10 you should definitely use the Windows.Storage library which contains the SystemPhotoProperties, SystemMusicProperties etc. https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-getting-file-properties

    And finally, I posted a much better solution that uses WindowsAPICodePack there

    0 讨论(0)
  • 2020-11-22 03:39

    There's a CodeProject article for an ID3 reader. And a thread at kixtart.org that has more information for other properties. Basically, you need to call the GetDetailsOf() method on the folder shell object for shell32.dll.

    0 讨论(0)
  • 2020-11-22 03:41

    For those of not crazy about VB, here it is in c#:

    Note, you have to add a reference to Microsoft Shell Controls and Automation from the COM tab of the References dialog.

    public static void Main(string[] args)
    {
        List<string> arrHeaders = new List<string>();
    
        Shell32.Shell shell = new Shell32.Shell();
        Shell32.Folder objFolder;
    
        objFolder = shell.NameSpace(@"C:\temp\testprop");
    
        for( int i = 0; i < short.MaxValue; i++ )
        {
            string header = objFolder.GetDetailsOf(null, i);
            if (String.IsNullOrEmpty(header))
                break;
            arrHeaders.Add(header);
        }
    
        foreach(Shell32.FolderItem2 item in objFolder.Items())
        {
            for (int i = 0; i < arrHeaders.Count; i++)
            {
                Console.WriteLine(
                  $"{i}\t{arrHeaders[i]}: {objFolder.GetDetailsOf(item, i)}");
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:41

    Thank you guys for this thread! It helped me when I wanted to figure out an exe's file version. However, I needed to figure out the last bit myself of what is called Extended Properties.

    If you open properties of an exe (or dll) file in Windows Explorer, you get a Version tab, and a view of Extended Properties of that file. I wanted to access one of those values.

    The solution to this is the property indexer FolderItem.ExtendedProperty and if you drop all spaces in the property's name, you'll get the value. E.g. File Version goes FileVersion, and there you have it.

    Hope this helps anyone else, just thought I'd add this info to this thread. Cheers!

    0 讨论(0)
  • 2020-11-22 03:42
    • After looking at a number of solutions on this thread and elsewhere the following code was put together. This is only to read a property.
    • I could not get the Shell32.FolderItem2.ExtendedProperty function to work, it is supposed to take a string value and return the correct value and type for that property... this was always null for me and developer reference resources were very thin.
    • The WindowsApiCodePack seems to have been abandoned by Microsoft which brings us the code below.

    Use:

    string propertyValue = GetExtendedFileProperty("c:\\temp\\FileNameYouWant.ext","PropertyYouWant");
    
    1. Will return you the value of the extended property you want as a string for the given file and property name.
    2. Only loops until it found the specified property - not until all properties are discovered like some sample code
    3. Will work on Windows versions like Windows server 2008 where you will get the error "Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'" if just trying to create the Shell32 Object normally.

      public static string GetExtendedFileProperty(string filePath, string propertyName)
      {
          string value = string.Empty;
          string baseFolder = Path.GetDirectoryName(filePath);
          string fileName = Path.GetFileName(filePath);
      
          //Method to load and execute the Shell object for Windows server 8 environment otherwise you get "Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'"
          Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
          Object shell = Activator.CreateInstance(shellAppType);
          Shell32.Folder shellFolder = (Shell32.Folder)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { baseFolder });
      
          //Parsename will find the specific file I'm looking for in the Shell32.Folder object
          Shell32.FolderItem folderitem = shellFolder.ParseName(fileName);
          if (folderitem != null)
          {
              for (int i = 0; i < short.MaxValue; i++)
              {
                  //Get the property name for property index i
                  string property = shellFolder.GetDetailsOf(null, i);
      
                  //Will be empty when all possible properties has been looped through, break out of loop
                  if (String.IsNullOrEmpty(property)) break;
      
                  //Skip to next property if this is not the specified property
                  if (property != propertyName) continue;    
      
                  //Read value of property
                  value = shellFolder.GetDetailsOf(folderitem, i);
              }
          }
          //returns string.Empty if no value was found for the specified property
          return value;
      }
      
    0 讨论(0)
提交回复
热议问题