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

后端 未结 10 2438
我在风中等你
我在风中等你 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: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;
      }
      

提交回复
热议问题