2017-05-14 17 views
0

因此,我試圖爲我的應用程序實現自動更新,from this threadSystem.BadImageFormatException得到AssemblyVersion時

我已經上傳安裝文件到我的服務器,當我試着去獲取文件,我得到System.BadImageFormatException,更確切的的AssemblyVersion:

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. 

我讀過,這是因爲一個x64應用程序試圖運行一個x86 DLL,但這不應該是這種情況,因爲它是完全相同的應用程序試圖從中獲取信息?

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(); 
     } 

我的設置是x86,應用程序也是x86。

+1

的.msi不是.NET文件,你爲什麼會用'AssemblyVersion'檢查呢? –

+0

哇,我很愚蠢,所以用.msi文件做這件事是不可能的? –

+0

[使用'FileVersionInfo'](http://stackoverflow.com/a/1537764/107625)怎麼樣? –

回答

1

劃傷我的舊回答,似乎沒有簡單的方法來設置MSI軟件包的文件版本。然後,解決方案需要更多的工作,並且需要外部依賴性,實際上是查詢MSI文件屬性表。

對於此代碼工作(是的話,我已經檢查了它針對適當的MSI文件),你需要的組件從WiX Toolset SDKMicrosoft.Deployment.WindowsInstaller.dllMicrosoft.Deployment.WindowsInstaller.Linq.dll。如果您安裝了整個工具箱,則可以在壓縮版本的their releases中找到這些文件,或者在安裝文件夾中找到這些文件。

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}"); 
} 
+0

謝謝,雖然每個字段都是空白,當我在我的MSI文件上執行此操作時, –

+0

是的,似乎在二進制文件上獲取真正的FileVersion屬性非常困難,因此我使用另一種方法更新了我的答案,該方法從msi相反地​​顯示。 –

+0

非常感謝,完美的作品! :) –

相關問題