param( [Parameter(Mandatory)][string]$Path, [Parameter(Mandatory)][string]$Version, [Parameter(Mandatory)][string]$NumericVersion ) $ErrorActionPreference = 'Stop' Add-Type -TypeDefinition @' using System; using System.IO; using System.Text; using System.Diagnostics; using System.ComponentModel; using System.Runtime.InteropServices; public static class RuneStoneVersionResource { [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)] static extern IntPtr BeginUpdateResource(string path, bool deleteExisting); [DllImport("kernel32.dll", SetLastError=true)] static extern bool UpdateResource(IntPtr handle, IntPtr type, IntPtr name, ushort language, byte[] data, uint size); [DllImport("kernel32.dll", SetLastError=true)] static extern bool EndUpdateResource(IntPtr handle, bool discard); static void Align(BinaryWriter writer) { while (writer.BaseStream.Position % 4 != 0) writer.Write((byte)0); } static byte[] Text(string value) { return Encoding.Unicode.GetBytes(value + "\0"); } static byte[] Block(string key, ushort type, byte[] value, ushort valueLength, params byte[][] children) { using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) { writer.Write((ushort)0); writer.Write(valueLength); writer.Write(type); writer.Write(Text(key)); Align(writer); writer.Write(value); foreach (var child in children) { Align(writer); writer.Write(child); } long length = stream.Length; stream.Position = 0; writer.Write(checked((ushort)length)); return stream.ToArray(); } } static byte[] StringValue(string key, string value) { var bytes = Text(value); return Block(key, 1, bytes, checked((ushort)(bytes.Length / 2))); } public static void Set(string path, string version, string numeric) { var p = Array.ConvertAll(numeric.Split('.'), UInt16.Parse); if (p.Length != 4) throw new ArgumentException("Use four Windows version parts."); var previous = FileVersionInfo.GetVersionInfo(path); byte[] fixedInfo; using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) { uint high = ((uint)p[0] << 16) | p[1], low = ((uint)p[2] << 16) | p[3]; foreach (uint value in new uint[] { 0xFEEF04BD, 0x10000, high, low, high, low, 0x3F, 0, 4, 1, 0, 0, 0 }) writer.Write(value); fixedInfo = stream.ToArray(); } var strings = Block("StringFileInfo", 1, new byte[0], 0, Block("040904B0", 1, new byte[0], 0, StringValue("CompanyName", previous.CompanyName ?? "RuneStone"), StringValue("FileDescription", previous.FileDescription ?? "RuneStone"), StringValue("ProductName", "RuneStone"), StringValue("OriginalFilename", "runestone.exe"), StringValue("FileVersion", version), StringValue("ProductVersion", version))); var vars = Block("VarFileInfo", 1, new byte[0], 0, Block("Translation", 0, new byte[] { 9, 4, 0xB0, 4 }, 4)); var data = Block("VS_VERSION_INFO", 0, fixedInfo, 52, strings, vars); var handle = BeginUpdateResource(path, false); if (handle == IntPtr.Zero) throw new Win32Exception(); if (!UpdateResource(handle, (IntPtr)16, (IntPtr)1, 0, data, (uint)data.Length)) { var error = new Win32Exception(); EndUpdateResource(handle, true); throw error; } if (!EndUpdateResource(handle, false)) throw new Win32Exception(); } } '@ $resolvedPath = (Resolve-Path -LiteralPath $Path).Path [RuneStoneVersionResource]::Set($resolvedPath, $Version, $NumericVersion) $actual = [Diagnostics.FileVersionInfo]::GetVersionInfo($resolvedPath) if ($actual.ProductVersion -ne $Version -or $actual.FileVersion -ne $Version) { throw 'The executable version resource does not match the release version.' } $actual | Select-Object FileVersion, ProductVersion, FileMajorPart, FileMinorPart, FileBuildPart, FilePrivatePart