2011-02-05 106 views
10

我有一個.NET 4 WPF應用程序,使用MSI安裝,通過Visual Studio安裝項目生成。一切都很好,除了我缺少點擊一次部署功能來檢查加載的新版本應用程序並下載/安裝它們。我從Click Once Deployment切換開來,因爲它似乎是一個半熟的解決方案,它使您可以簡單地做一些簡單的事情,例如在啓動時運行您的應用程序。代碼檢查更新,安裝新版本的應用程序

我想知道是否有任何形式的教程或代碼,任何人都可以向我展示如何處理檢查應用程序的新版本,下載新版本的應用程序以及在舊的應用程序上安裝新應用程序一。這似乎是大多數WPF應用程序想要的東西,我很驚訝我在谷歌上找不到任何關於這個的東西。

回答

13

沒有這種內置或現成的工具。在你的應用程序啓動時,你可以運行你的代碼,它遵循以下步驟

  1. 取你的http://myserver.com/myproduct/update.xml,在那裏你會保持你的最新的一個新的版本更新和URL設置msi文件
  2. 如果有可用更新,是不同的,那麼當前運行的版本,然後使用Web客戶端下載的文件和它存儲在臨時文件夾
  3. 然後,您可以創建具有以下字符串的批處理文件並將其保存在臨時文件夾
msiexec /u {your product code} 
msiexec /i ..path to your new msi 

最後使用Process.Start執行您的批處理文件並退出您的應用程序。

+0

感謝@Akash Kava我現在已經很近了,但在msiexec命令行卸載/安裝中看到了我遇到的問題。 – Justin 2011-02-05 20:18:58

2

查看Scott Hanselman的博客文章manually update via clickonce

+2

我後說,我沒有使用點擊一次部署... – Justin 2011-02-05 18:05:18

+0

明白了你'不使用ClickOnce,但是,我的搜索把我帶到了這裏,這將幫助*使用CO的其他人:如何以編程方式檢查應用程序更新使用ClickOnce部署API http://msdn.microsoft.com/zh-cn/ us/library/ms404263.aspx – 2013-09-17 17:11:41

13

得到它的工作,這裏是代碼,這樣其他人不需要推倒重來......

public class VersionHelper 
{ 
    private string MSIFilePath = Path.Combine(Environment.CurrentDirectory, "HoustersCrawler.msi"); 
    private string CmdFilePath = Path.Combine(Environment.CurrentDirectory, "Install.cmd"); 
    private string MsiUrl = String.Empty; 

    public bool CheckForNewVersion() 
    { 
     MsiUrl = GetNewVersionUrl(); 
     return MsiUrl.Length > 0; 
    } 

    public void DownloadNewVersion() 
    { 
     DownloadNewVersion(MsiUrl); 
     CreateCmdFile(); 
     RunCmdFile(); 
     ExitApplication(); 
    } 

    private string GetNewVersionUrl() 
    { 
     var currentVersion = Convert.ToInt32(ConfigurationManager.AppSettings["Version"]); 
     //get xml from url. 
     var url = ConfigurationManager.AppSettings["VersionUrl"].ToString(); 
     var builder = new StringBuilder(); 
     using (var stringWriter = new StringWriter(builder)) 
     { 
      using (var xmlReader = new XmlTextReader(url)) 
      { 
       var doc = XDocument.Load(xmlReader); 
       //get versions. 
       var versions = from v in doc.Descendants("version") 
           select new 
           { 
            Name = v.Element("name").Value, 
            Number = Convert.ToInt32(v.Element("number").Value), 
            URL = v.Element("url").Value, 
            Date = Convert.ToDateTime(v.Element("date").Value) 
           }; 
       var version = versions.ToList()[0]; 
       //check if latest version newer than current version. 
       if (version.Number > currentVersion) 
       { 
        return version.URL; 
       } 
      } 
     } 
     return String.Empty; 
    } 

    private void DownloadNewVersion(string url) 
    { 
     //delete existing msi. 
     if (File.Exists(MSIFilePath)) 
     { 
      File.Delete(MSIFilePath); 
     } 
     //download new msi. 
     using (var client = new WebClient()) 
     { 
      client.DownloadFile(url, MSIFilePath); 
     } 
    } 

    private void CreateCmdFile() 
    { 
     //check if file exists. 
     if (File.Exists(CmdFilePath)) 
      File.Delete(CmdFilePath); 
     //create new file. 
     var fi = new FileInfo(CmdFilePath); 
     var fileStream = fi.Create(); 
     fileStream.Close(); 
     //write commands to file. 
     using (TextWriter writer = new StreamWriter(CmdFilePath)) 
     { 
      writer.WriteLine(@"msiexec /i HoustersCrawler.msi /quiet"); 
     } 
    } 

    private void RunCmdFile() 
    {//run command file to reinstall app. 
     var p = new Process(); 
     p.StartInfo = new ProcessStartInfo("cmd.exe", "/c Install.cmd"); 
     p.StartInfo.CreateNoWindow = true; 
     p.Start(); 
     //p.WaitForExit(); 
    } 

    private void ExitApplication() 
    {//exit the app. 
     Application.Current.Shutdown(); 
    } 
} 
相關問題