System.BadImageFormatException when getting the AssemblyVersion

0

So Im trying to implement a auto-updater for my application, from this thread.

I've uploaded my setup file to my server and when Im trying to get the AssemblyVersion of the file I get System.BadImageFormatException, more exactly:

System.BadImageFormatException occurred
  HResult=0x8007000B
  Message=Det går inte att läsa in filen eller sammansättningen Setup1.msi eller ett av dess beroenden. Ett försök att läsa in ett program med ogiltigt format gjordes.
  Source=mscorlib
  StackTrace:
   at System.Reflection.AssemblyName.nGetFileInformation(String s)
   at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
   at WindowsFormsApp1.Form1..ctor() in C:\Users\Gustav\Documents\Visual Studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:line 50
   at WindowsFormsApp1.Program.Main() in C:\Users\Gustav\Documents\Visual Studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\Program.cs:line 22

Inner Exception 1:
BadImageFormatException: Det går inte att läsa in filen eller sammansättningen Setup1.msi eller ett av dess beroenden. Ett försök att läsa in ett program med ogiltigt format gjordes.

I've read that this is because of an x64 application trying to run a x86 DLL, but this shouldn't be the case since it's the exact same application im trying to get the info from?

    string remoteUri = "http://<URL>/downloads/";
    string fileName = "Setup1.msi", myStringWebResource = null;
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    // Concatenate the domain with the Web resource filename.
    myStringWebResource = remoteUri + fileName;
        myWebClient.DownloadFile(myStringWebResource, fileName);

        if (AssemblyName.GetAssemblyName("Setup1.msi").Version > Assembly.GetExecutingAssembly().GetName().Version)
        {
            logger.Add("Update found!");
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "Setup1.msi";
            Process.Start(startInfo);
            this.Close();
        }

My setup is x86 and the application is also x86.

c#
asked on Stack Overflow May 14, 2017 by Gustav P Svensson • edited May 23, 2017 by Community

1 Answer

1

Scratch my old answer, it seems that there is no easy way to set the file version for MSI packages. The solution then, which is a bit more work and requires an external dependency is to actually query the MSI file property table.

For this code to work (it does, I've checked it against a proper MSI file) you'll need the assemblies Microsoft.Deployment.WindowsInstaller.dll and Microsoft.Deployment.WindowsInstaller.Linq.dll from the WiX Toolset SDK. You can find the files in the zipped version of their releases or in the installation folder if you install the whole toolset.

using(var database = new QDatabase("node-v6.10.3-x64.msi", DatabaseOpenMode.ReadOnly)) {
    var productVersion = database.Properties.AsEnumerable().FirstOrDefault(p => p.Property == "ProductVersion");

    if(productVersion != null)
        Console.WriteLine($"Product version is {productVersion.Value}");
}
answered on Stack Overflow May 14, 2017 by Karl-Johan Sjögren • edited May 14, 2017 by Karl-Johan Sjögren

User contributions licensed under CC BY-SA 3.0