0

Good day.

We are creating a Control System sales in school cafeterias in C # with WPF and this system will be implemented in 18 schools in our network. ( Schools are scattered throughout the city and , therefore, the system updates will be made automatically by the Internet ) .

Researched ClickOnce ( got to develop something like that ) and we understand that this type of update is not feasible .

We created a system update using DotNetZip API but errors occurred saving file in the directory where the system files as incompatibility among other problems was installed .

My question is as follows :

Does anyone have any experience in this matter that could share with me , or any example of situation ( like it is) so we can apply here in our system ?

I'll put here the snippet of code that updates the system to create , analyze it if anyone wants it , it will be easier to understand the problem .

The logic of this code search an XML file available system version and compares it with the version of the assembly . If the version is higher, then down to a temp directory update files and transfer to the directory where the application was installed . The problem that occurs is that some types of files appear as if they are running even after they have been finalized with the main application .

I appreciate the help .

Main form that calls the updater

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var doc = new XmlDocument();
    doc.Load("http://www.meusite.org.br/Cantina/arquivoXML.xml");
        if (doc.DocumentElement != null)
        {
            var node = doc.DocumentElement.SelectSingleNode("/Application/Version");
            var node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
            if (node != null)
            {
                var version = node.InnerText;
                var versionAssembly =     System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                if (Convert.ToDouble(version) > Convert.ToDouble(versionAssembly))
                {
                    Process.Start("Updater.exe");
                }
            }
        }
     }

Updater system:

using System.Diagnostics;
using Ionic.Zip;
using System.Xml;
using System.IO;


namespace Updater
{
public class Program
{
    private static void Main()
    {
   Process[] process = Process.GetProcessesByName("IASD.ASCS.WPF");
    foreach (Process proc in process)
    {
        if (!proc.HasExited)
            proc.Kill();
    }
    XmlDocument doc = new XmlDocument();
    doc.Load("http://www.meusite.org.br/Cantina/arquivoXML.xml");
    if (doc.DocumentElement != null)
    {
        XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
        if (node1 != null)
        {
            string zipfile = node1.InnerText;
            const string end = ("http://www.meusite.org.br/Cantina/");
            string file = (end + zipfile);
            ZipFile zipFile = ZipFile.Read(file);
            {
                foreach (var zipEntry in zipFile)
                {
                    zipEntry.Extract(@"c:\IASD\CantinaEscolar\Temp",ExtractExistingFileAction.OverwriteSilently);
                }
            }
            string dirTemp = @"c:\IASD\CantinaEscolar\Temp";
            string dirInstalacao = @"c:\IASD\CantinaEscolar\";
            string[] arquivos = Directory.GetFiles(dirTemp);
            foreach (string item in arquivos)
            {
                string nomedoarquivo = Path.GetFileName(item);
                if (nomedoarquivo != null)
                {
                    string destino = Path.Combine(dirInstalacao, nomedoarquivo);
                    File.Copy(item, destino, true);
                }
            }
            string[] arquivosApagar = Directory.GetFiles(dirTemp);
            foreach (string item in arquivosApagar)
            {
                File.Delete(item);
            }
            Process.Start("IASD.ASCS.WPF.exe");
        }
    }
    const string nomeExecutavel2 = "Updater.exe";
    foreach (Process pr2 in Process.GetProcessesByName(nomeExecutavel2))
      {
        if (!pr2.HasExited) pr2.Kill();
      }
    }
  }
}

In this example I am determining which location the system will be installed but will I need to provide the user installs in any directory on your computer (which is another problem that I could not work around).

Paulo Romeiro
  • 191
  • 1
  • 7

2 Answers2

1

When you are doing an update and the application is still running you can do it anyway but you have to rename all files and then paste the new ones. The application will keep running normally till the next application restart.

Our process looks like this:

  1. Check for updates
  2. Download update zip
  3. Rename all files to *.old
  4. Extract the zip file to the application location
  5. Close (kill) and restart application
  6. Delete *.old files on startup

We never had any problems with this behaviour, but maybe you wan't to implement a mutex for your application so you can be sure there is only one instance running at the same time.

Community
  • 1
  • 1
Staeff
  • 4,994
  • 6
  • 34
  • 58
1

After you start off the download in the Window_Loaded event, the original process keeps running. This when the downloader tries to replace the exe file and any files it is using it will fail.

After starting the updater, Process.Start("Updater.exe"); you need to exit the current process and make sure it is not running at all. You can do this by adding the following line:

if (Convert.ToDouble(version) > Convert.ToDouble(versionAssembly))
{
    Process.Start("Updater.exe");
    Application.Exit(); // Assumes a WinForms app
}
simon at rcl
  • 7,326
  • 1
  • 17
  • 24