Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LegalCopyRight is always empty in FileVersionInfo C#?

I have a SFX(self-extracting executable) file in windows (Created with zip tools like 7z, WinRar, ....) with the following details:

File Details

I want to get CopyRight text in C#, So I wrote the following code:

var fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
Console.Write(fileVersionInfo.LegalCopyright)

fileVersionInfo.LegalCopyright is always empty! What's the problem?

Edit:
My original Code:

var fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath1);
var properties = typeof(FileVersionInfo).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var propertyInfo in properties)
{
    var value = propertyInfo.GetValue(fileVersionInfo);
    Console.WriteLine("{0} = {1}", propertyInfo.Name, value);
}
Console.ReadKey();

The result:

File Details

like image 1000
Mohammad Dayyan Avatar asked Dec 19 '25 06:12

Mohammad Dayyan


2 Answers

(My reputation is too low to make a comment, so i post it here)

I have just tested the following code, and it works normally for me.

var fileVersionInfo = FileVersionInfo.GetVersionInfo(@"C:\Users\usr\Desktop\Game\steamIntegration\steam_api.dll");
Console.Write(fileVersionInfo.LegalCopyright);
Console.ReadLine();

Maybe your permissions are not sufficient enough for that file. Add a *.manifest to your project change the requestedExecutionLevel to:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Maybe that solves your problem.

like image 124
Lars Crown Avatar answered Dec 21 '25 20:12

Lars Crown


The behavior you observe is due to a shortcoming in the implementation of the private function GetVersionInfoForCodePage in line 411 of the Microsoft.NET framework class FileVersionInfo, currently in version 4.6.2 and likely much earlier:

// fileVersion is chosen based on best guess. Other fields can be used if appropriate. 
return (fileVersion != string.Empty);

This citation from reference source (comment theirs) means that the function will give up an otherwise correctly guessed codepage-specific version info, if its fileVersion member is empty (that of the block, not that in the header, which adds to the confusion).

This is the case with your exe file:

version info as ResHacker shows it

When we patch the framework to use this instead...

return (productVersion != string.Empty);

...it works as expected (tested in both console and Windows application):

output from patched version

So two options:

  1. Compile the exe so that its FileVersion does not end up empty. Hopefully it is not the fault of your compression tool to not transport this information.
  2. File a bug with Microsoft. What I gleaned from their reference source licence, it does not permit to include derived works in any product.
like image 31
Cee McSharpface Avatar answered Dec 21 '25 19:12

Cee McSharpface



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!