0

I am trying to make a Version resource tool - pretty much because I came across the FileVersionInfo class recently. I am using FileVersionInfo.GetVersionInfo to obtain an executable's version information through code. Now, I would like to use code to update the version information of an executable file. I wasn't sure if there was a built in class as there is for obtaining the information.

If not, I got an idea to use something similar to the following code in order to replace the file's information. I used to do something similar to this in VB6 - which is where I got the idea from.

File.ReadAllBytes(@"\MyFile.exe")
// Convert the data into a string
// Use the Replace function to find original version information (obtained via FileVersionInfo), and replace it with new information to my liking.

Would this work? If not, might anyone have some better ideas?

Thanks, Evan

  • Isnt the version information compiled as a part of the executable? You can't just update the version information without recompiling the target. – Tejs May 06 '11 at 17:34
  • Why don't you simply compile the code again and update the Assembly information? Honestly I don't believe what you want to do is possible. The version information is aviable because Microsoft made it accessible and read-only for reason. – Security Hound May 06 '11 at 17:35
  • 1
    Take a look at the answer to this similar question: http://stackoverflow.com/questions/284258/how-do-i-set-the-version-information-for-an-existing-exe-dll – Josh Gallagher May 06 '11 at 17:35
  • Also note that you won't be able to change the version of a signed assembly in any way that the .NET CLR recognises as this would bypass various security features. – Josh Gallagher May 06 '11 at 17:37
  • I dont think this would work because exe contain binaries and version might not be existing in the way you are expecting it to be. Did you open the exe and search the version info from the binaries? – FIre Panda May 06 '11 at 17:37

2 Answers2

3

Please don't do what you're trying to, even if you manage to find a way. When you include assemblies in many cases the version number is required. If you go around setting versions for an assembly on a whim it could wreak havoc with people who rely on your assemblies.

<compilation debug = "true" targetFramework = "4.0">
    <assemblies>
        <add assembly = "System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly = "System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly = "System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
</compilation>
Yuck
  • 49,664
  • 13
  • 105
  • 135
  • This seems like the best way to do it: http://www.codeproject.com/KB/library/ResourceLib.aspx unfortunately, this is very tough to understand. In time, however, I'm sure it will do the right thing. –  May 06 '11 at 18:36
0

I don't think reading the whole file and matching the VERSIONINFO block will be that easy. If I recall correctly, the resource data is not made only of strings, there are binary chunks here and there.

An alternative would be to p/invoke BeginUpdateResource(), UpdateResource() and EndUpdateResource(). See this article for a code example and some pointers.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479