Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic update a Windows application

Tags:

c#

auto-update

How do I develop my Windows application so it will auto update on the client machine, like Firefox, Skype, etc.?

Is there any simple approach or any open source library which help me to do it just following some steps or a few lines of code?

like image 994
Thomas Avatar asked Jan 22 '11 18:01

Thomas


1 Answers

Use MD5-Update it easy only need add 5 lines at your application, no configuration need in your app only add library and publish the files.

MD5-Update

1. Your need a web server with PHP for publish your files please include updt.exe.

WebServer

2. Add index.php for make list of update files. aviable on github repository https://github.com/jrz-soft-mx/MD5-Update/blob/main/Tools/Tools.zip o create new app with this code.

<?php
$_dat = array();
$_dir=new RecursiveDirectoryIterator(".");
foreach (new RecursiveIteratorIterator($_dir) as $_itm) {
    $_fil = str_replace(".".DIRECTORY_SEPARATOR, "", $_itm);
    if(!is_dir($_fil) && $_fil != "index.php"){     
        $_dat[]=array('StrFil' => "$_fil", 'StrMd5' => strtoupper(md5_file($_fil)), 'lonSiz' => filesize($_fil));
    }
}
echo json_encode($_dat, JSON_UNESCAPED_UNICODE);
?>

enter image description here

3. Add nuget repository at your proyect

PM> Install-Package MD5.Update

4. Call the library when your app stars, with your update folder url, update all files and download your new app on updt folder, for replace your app need updt.exe

string strUrl = "http://yourdomain.com/app/";
if (MD5Update.MD5Update.Check(strUrl, true))
{
    Process.Start(AppDomain.CurrentDomain.BaseDirectory + @"updt.exe", AppDomain.CurrentDomain.FriendlyName + " " + Process.GetCurrentProcess().ProcessName);
    Application.Exit();
}

enter image description here

enter image description here

5. updt.exe for replace the current app with the new app updt folder to app. aviable on github repository https://github.com/jrz-soft-mx/MD5-Update/blob/main/Tools/Tools.zip o create new app with this code.

try
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    List<string> lisArg = Environment.GetCommandLineArgs().ToList();
    if (lisArg.Count < 2)
    {
        MessageBox.Show("Please provide App Excutable Name and Procees name");
        Application.Exit();
        return;
    }
    string strAppName = lisArg[1];
    string strAppProcees = lisArg[2];
    Process[] lisPro = Process.GetProcessesByName(strAppProcees);
    foreach (Process Pro in lisPro)
    {
        if (Pro.Id != Process.GetCurrentProcess().Id)
        {
            Pro.Kill();
            Thread.Sleep(1000);
        }
    }
    string strAppMain = AppDomain.CurrentDomain.BaseDirectory + strAppName;
    string strAppUpdate = AppDomain.CurrentDomain.BaseDirectory + @"updt\" + strAppName;
    if (!File.Exists(strAppMain))
    {
        MessageBox.Show("App Excutable dosent exists");
        Application.Exit();
        return;
    }
    if (!File.Exists(strAppUpdate))
    {
        MessageBox.Show("App Excutable Updated dosent exists");
        Application.Exit();
        return;
    }
    File.Copy(strAppUpdate, strAppMain, true);
    long fileSize = 0;
    FileInfo currentFile = new FileInfo(strAppMain);
    while (fileSize < currentFile.Length)
    {
        fileSize = currentFile.Length;
        Thread.Sleep(1000);
        currentFile.Refresh();
    }
    Process.Start(strAppMain);
}
catch (Exception Ex)
{
    MessageBox.Show("An error ocurred");
    File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"updt_" + DateTime.Now.ToString("yyyyMMddTHHmmss")  + " .txt", Ex.ToString());
    Application.Exit();
}
like image 155
jrz.soft.mx Avatar answered Sep 21 '22 11:09

jrz.soft.mx