0

I'm trying to extract the value "file version" using c#, but it keeps on coming out empty. All other values seems to be read ok. Anyone got any tips?

enter image description here

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);
                Console.WriteLine(property + " -> " + value);
            }
        }
        //returns string.Empty if no value was found for the specified property
        return value;
    }
Al Fahad
  • 2,378
  • 5
  • 28
  • 37
Nesvik
  • 1
  • Try to use FileInfo class. 'FileInfo fileInfo = new FileInfo(strFilename);' – PWND Nov 14 '18 at 13:37
  • Looking at the documentation of GetDetailsOf() function, i don't feel it can return FileVersion. Just try System.Diagnostics.FileVersionInfo Class. Refer : https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.fileversioninfo?view=netframework-4.7.2 – Praveen Rai Nov 14 '18 at 13:48
  • FileVersionInfo can only work on executable files, this isn't one. The 2nd argument is [documented here](https://learn.microsoft.com/en-us/previous-versions/tn-archive/ee176615(v=technet.10)). Why index 32 doesn't work for you is hard to guess, you ought to mention what you get. [Look here](https://stackoverflow.com/questions/35451540/extracting-windows-file-properties-custom-properties-c-sharp). – Hans Passant Nov 14 '18 at 16:51

1 Answers1

1

Did you try FileVersionInfo.FileVersion

FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
           "Version number: " + myFileVersionInfo.FileVersion);
Antoine V
  • 6,998
  • 2
  • 11
  • 34