I have a SFX(self-extracting executable) file in windows (Created with zip tools like 7z
, WinRar
, ....) with the following 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:
Finally I could find a solution:
1. First install the following package:
Microsoft.WindowsAPICodePack.Shell
It has a dependency package, Nuget install it automatically Microsoft.WindowsAPICodePack.Core
2. Now we can get file properties as the following code
using System;
using System.Reflection;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main()
{
const string filePath1 = @"C:\Users\Mohammad\Downloads\Test\.....exe";
var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellObject.FromParsingName(filePath1);
foreach (var propertyInfo in typeof(ShellProperties.PropertySystem).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var shellProperty = propertyInfo.GetValue(shellFile.Properties.System, null) as IShellProperty;
if (shellProperty?.ValueAsObject == null) continue;
var shellPropertyValues = shellProperty.ValueAsObject as object[];
if (shellPropertyValues != null && shellPropertyValues.Length > 0)
{
foreach (var shellPropertyValue in shellPropertyValues)
Console.WriteLine("{0} = {1}", propertyInfo.Name, shellPropertyValue);
}
else
Console.WriteLine("{0} = {1}", propertyInfo.Name, shellProperty.ValueAsObject);
}
Console.ReadKey();
}
}
}