Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code to check for updates, install new version of app

Tags:

wpf

.net-4.0

I have a .NET 4 WPF app that gets installed using an MSI, generated via a visual studio setup project. Everything works great, except that I'm missing the Click Once Deployment feature that checks for new versions of the app on load and downloads/installs them. I switched away from Click Once Deployment because it seems to be a half-baked solution that makes you do hacks just to do simple things like have your app run on startup.

I was wondering if there is any sort of tutorial or code anyone can show me that lays out how to handle checking for new versions of the app, downloading the new version of the app, and installing the new app over the old one. This seems like something that most WPF apps would want to have, I'm surprised that I can't find anything about this on google.

like image 610
Justin Avatar asked Feb 05 '11 14:02

Justin


1 Answers

Got it working, here is the code so that others don't need to reinvent the wheel...

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();
    }
}
like image 80
Justin Avatar answered Oct 12 '22 11:10

Justin