Пример #1
0
        private static void PerformCheck(object state)
        {
            try
            {
                CheckForNewRelease();
            }
            catch
            {
                Log.Warn("Failed during CheckForNewRelease.");
                return;
            }

            try
            {
                bool autoUpdate = (state as CommandLineArgs).AutomaticallyUpdate;
                if (autoUpdate)
                {
                    {
                        String url      = Settings.UpdateSource;
                        String contents = Download(url);

                        if (!String.IsNullOrEmpty(contents))
                        {
                            TerminalsUpdates updates =
                                (TerminalsUpdates)Serialize.DeSerializeXml(contents, typeof(TerminalsUpdates));
                            if (updates != null)
                            {
                                String currentMD5 = GetMD5HashFromFile(AssemblyInfo.Title + ".exe");
                                if (currentMD5 != null)
                                {
                                    //get the latest build
                                    DataRow row = updates.Tables[0].Rows[0];
                                    String  md5 = (row["MD5"] as string);
                                    if (!md5.Equals(currentMD5))
                                    {
                                        String version = (row["version"] as String);
                                        if (!Directory.Exists("Builds"))
                                        {
                                            Directory.CreateDirectory("Builds");
                                        }

                                        String finalFolder = @"Builds\" + version;
                                        if (!Directory.Exists(finalFolder))
                                        {
                                            Directory.CreateDirectory(finalFolder);
                                        }

                                        String filename = String.Format("{0}.zip", version);
                                        filename = @"Builds\" + filename;
                                        Boolean downloaded = true;

                                        if (!File.Exists(filename))
                                        {
                                            downloaded = DownloadNewBuild((row["Url"] as String), filename);
                                        }

                                        if (downloaded && File.Exists(filename))
                                        {
                                            //ICSharpCode.SharpZipLib.Zip.FastZipEvents evts = new ICSharpCode.SharpZipLib.Zip.FastZipEvents();
                                            FastZip fz = new FastZip();
                                            fz.ExtractZip(filename, finalFolder, null);

                                            if (
                                                MessageBox.Show(
                                                    "A new build is available, would you like to install it now",
                                                    "New Build", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                            {
                                                DirectoryInfo parent = FindFileInFolder(new DirectoryInfo(finalFolder),
                                                                                        AssemblyInfo.Title + ".exe");
                                                if (parent == null)
                                                {
                                                    return;
                                                }

                                                finalFolder = parent.FullName;

                                                File.Copy(Settings.CONFIG_FILE_NAME,
                                                          Path.Combine(finalFolder, Settings.CONFIG_FILE_NAME), true);
                                                File.Copy(AssemblyInfo.Title + ".log4net.config",
                                                          Path.Combine(finalFolder,
                                                                       AssemblyInfo.Title + ".log4net.config"), true);

                                                String temp =
                                                    Environment.GetFolderPath(
                                                        Environment.SpecialFolder.LocalApplicationData);
                                                String updaterExe = Path.Combine(temp,
                                                                                 AssemblyInfo.Title + "Updater.exe");
                                                if (
                                                    File.Exists(Path.Combine(finalFolder,
                                                                             AssemblyInfo.Title + "Updater.exe")))
                                                {
                                                    File.Copy(
                                                        Path.Combine(finalFolder, AssemblyInfo.Title + "Updater.exe"),
                                                        updaterExe, true);
                                                }

                                                //updaterExe = @"C:\Source\Terminals\Terminals\bin\Debug\";

                                                if (File.Exists(updaterExe))
                                                {
                                                    //String args = "\"" + finalFolder + "\" \"" + Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\"";
                                                    String args = String.Format("\"{0}\" \"{1}\"", finalFolder, AssemblyInfo.Location);
                                                    Process.Start(updaterExe, args);
                                                    Application.Exit();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Log.Error("Failed during update.", exc);
            }
        }