private void AddWin32VersionResource(string contractLocation, Assembly facade) { var versionInfo = FileVersionInfo.GetVersionInfo(contractLocation); var versionSerializer = new VersionResourceSerializer( true, versionInfo.Comments, versionInfo.CompanyName, versionInfo.FileDescription, _assemblyFileVersion == null ? versionInfo.FileVersion : _assemblyFileVersion.ToString(), versionInfo.InternalName, versionInfo.LegalCopyright, versionInfo.LegalTrademarks, versionInfo.OriginalFilename, versionInfo.ProductName, _assemblyFileVersion == null ? versionInfo.ProductVersion : _assemblyFileVersion.ToString(), facade.Version); using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream, Encoding.Unicode, true)) { versionSerializer.WriteVerResource(writer); var resource = new Win32Resource(); resource.Id = 1; resource.TypeId = 0x10; resource.Data = stream.ToArray().ToList(); facade.Win32Resources.Add(resource); } }
public static void SetInfo(string filename, Version version, Version fileVersion, SemanticVersion infoVersion, UpdateMethod updateMethod = UpdateMethod.ILDasm) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { try { var data = Win32Resource.ReadVersionResource(filename); var versionInfo = new VERSION_INFO_CHUNK(data); // Update fixed file info var info = CopyFrom <VS_FIXEDFILEINFO>(versionInfo.Value); if (fileVersion != null) { EncodeVersion(ref info.dwFileVersion, fileVersion); EncodeVersion(ref info.dwProductVersion, fileVersion); versionInfo.Value = CopyTo(info); } var trl = versionInfo.Children.First(c => c.Key == "VarFileInfo").Children.First(c => c.Key == "Translation"); var translation = BitConverter.ToUInt16(trl.Value, 0); foreach (var sfi in versionInfo.Children.Where(c => c.Key == "StringFileInfo")) { foreach (var table in sfi.Children) { foreach (var str in table.Children) { switch (str.Key) { case "FileVersion": if (fileVersion != null) { str.ValueText = fileVersion.ToString(3); } break; case "ProductVersion": if (infoVersion != null) { str.ValueText = infoVersion.ToString(); } else if (fileVersion != null) { str.ValueText = fileVersion.ToString(3); } break; } } } } data = versionInfo.GetData(); Win32Resource.WriteVersionResource(filename, data); } catch (Exception ex) { throw new Exception($"Error while updating version information for '{filename}'", ex); } } if (updateMethod == UpdateMethod.ILDasm) { var isDLL = (Path.GetExtension(filename) ?? "").Equals(".dll", StringComparison.InvariantCultureIgnoreCase); var tmpIL = Path.GetTempFileName(); if (!IL.Disassemble(filename, tmpIL)) { throw new Exception(string.Format("Failed to disassemble input DLL/EXE {0}.", filename)); } var data = File.ReadAllText(tmpIL); data = ReplaceAssemblyVersion(data, version); if (fileVersion != null) { data = AddOrReplaceAttribute(data, "AssemblyFileVersionAttribute", fileVersion.ToString()); } if (infoVersion != null) { data = AddOrReplaceAttribute(data, "AssemblyInformationalVersionAttribute", infoVersion.ToString()); } File.WriteAllText(tmpIL, data); var tmpDest = Path.GetTempFileName(); if (!IL.Assemble(tmpIL, tmpDest, isDLL, Path.ChangeExtension(tmpIL, "res"))) { throw new Exception(string.Format("Failed to assemble IL code.")); } ProgramHelper.FileCopy(tmpDest, filename); File.Delete(tmpDest); File.Delete(tmpIL); } else { var resolver = new TapMonoResolver(); var asm = AssemblyDefinition.ReadAssembly(filename, new ReaderParameters { AssemblyResolver = resolver, InMemory = true, ReadingMode = ReadingMode.Immediate }); if (version != null) { asm.Name.Version = version; } foreach (var attr in asm.CustomAttributes) { if (fileVersion != null && attr.AttributeType.FullName == typeof(AssemblyFileVersionAttribute).FullName) { attr.ConstructorArguments[0] = new CustomAttributeArgument(attr.ConstructorArguments[0].Type, fileVersion.ToString()); } if (infoVersion != null && attr.AttributeType.FullName == typeof(AssemblyInformationalVersionAttribute).FullName) { attr.ConstructorArguments[0] = new CustomAttributeArgument(attr.ConstructorArguments[0].Type, infoVersion.ToString()); } } using (var stream = File.Open(filename, FileMode.OpenOrCreate)) asm.Write(stream); } }
private Win32Resource ReadWin32ResourceDataEntry(MemoryCursor/*!*/ c, int position, string TypeName, int TypeID, string Name, int ID, int LanguageID) { Win32Resource rsrc = new Win32Resource(); rsrc.TypeName = TypeName; rsrc.TypeId = TypeID; rsrc.Name = Name; rsrc.Id = ID; rsrc.LanguageId = LanguageID; c = new MemoryCursor(c); c.Position = position; int dataRVA = c.ReadInt32(); int dataSize = c.ReadInt32(); rsrc.CodePage = c.ReadInt32(); c.Position = this.RvaToOffset(dataRVA); rsrc.Data = c.ReadBytes(dataSize); return rsrc; }
public void Initialise(Win32Resource resource) { // Create a byte stream from the data. this.pictureBoxPreview.Image = resource.BitmapData; }