// Copyright 2026 sjackson0109 — Apache License 2.0
using System.Diagnostics;
namespace RDPWrap.Common;
///
/// File-version reading helper. Mirrors the GetFileVersion function used in
/// RDPWInst.dpr and RDPConf MainUnit.pas.
///
public static class FileVersionHelper
{
///
/// Strongly-typed representation of a Windows file version.
///
public record FileVersionInfo(
ushort Major,
ushort Minor,
ushort Release,
ushort Build,
bool IsDebug,
bool IsPrerelease,
bool IsPrivate,
bool IsSpecial)
{
/// e.g. "10.0.26100.3476"
public override string ToString() => $"{Major}.{Minor}.{Release}.{Build}";
}
///
/// Returns the file version of , or null
/// if the file does not exist or has no version resource.
/// Uses the BCL which does
/// not require loading the DLL as executable — safe for locked DLLs.
///
public static FileVersionInfo? GetVersion(string filePath)
{
if (!File.Exists(filePath)) return null;
try
{
var fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath);
return new FileVersionInfo(
(ushort)(fvi.FileMajorPart),
(ushort)(fvi.FileMinorPart),
(ushort)(fvi.FileBuildPart),
(ushort)(fvi.FilePrivatePart),
fvi.IsDebug,
fvi.IsPreRelease,
fvi.IsPrivateBuild,
fvi.IsSpecialBuild);
}
catch
{
return null;
}
}
///
/// Convenience overload: resolves the path via
/// before reading.
///
public static FileVersionInfo? GetVersionExpanded(string pathWithEnvVars)
=> GetVersion(ArchHelper.ExpandPath(pathWithEnvVars));
}