mirror of
https://github.com/gwdevhub/Daybreak.git
synced 2026-09-13 20:09:21 +00:00
79 lines
2.0 KiB
C#
79 lines
2.0 KiB
C#
using PeNet;
|
|
|
|
namespace Daybreak.Utils;
|
|
|
|
/// <summary>
|
|
/// Cross-platform PE version reader using PeNet.
|
|
/// </summary>
|
|
internal static class PeVersionReader
|
|
{
|
|
/// <summary>
|
|
/// Reads the ProductVersion string from a PE file's version resource.
|
|
/// Returns null if the file doesn't exist or has no version info.
|
|
/// </summary>
|
|
public static string? GetProductVersion(string filePath)
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var peFile = new PeFile(filePath);
|
|
return peFile.Resources?.VsVersionInfo?.StringFileInfo?.StringTable
|
|
?.FirstOrDefault()?.ProductVersion;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the FileVersion string from a PE file's version resource.
|
|
/// Returns null if the file doesn't exist or has no version info.
|
|
/// </summary>
|
|
public static string? GetFileVersion(string filePath)
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var peFile = new PeFile(filePath);
|
|
return peFile.Resources?.VsVersionInfo?.StringFileInfo?.StringTable
|
|
?.FirstOrDefault()?.FileVersion;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the ProductName string from a PE file's version resource.
|
|
/// Returns null if the file doesn't exist or has no version info.
|
|
/// </summary>
|
|
public static string? GetProductName(string filePath)
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var peFile = new PeFile(filePath);
|
|
return peFile.Resources?.VsVersionInfo?.StringFileInfo?.StringTable
|
|
?.FirstOrDefault()?.ProductName;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|