コード例 #1
0
ファイル: UpgradeForm.cs プロジェクト: kiwiroy/ApsimX
        /// <summary>
        /// User has requested an upgrade.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnUpgrade(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count == 1)
            {
                try
                {
                    if (!checkBox1.Checked)
                    {
                        throw new Exception("You must agree to the license terms before upgrading.");
                    }

                    if (firstNameBox.Text == null || lastNameBox.Text == null ||
                        emailBox.Text == null || countryBox.Text == null)
                    {
                        throw new Exception("The mandatory details at the bottom of the screen (denoted with an asterisk) must be completed.");
                    }

                    Upgrade upgrade       = upgrades[listView1.SelectedIndices[0]];
                    string  versionNumber = upgrade.ReleaseDate.ToString("yyyy.MM.dd.") + upgrade.issueNumber;

                    if (MessageBox.Show("Are you sure you want to upgrade to version " + versionNumber + "?",
                                        "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Cursor.Current = Cursors.WaitCursor;

                        explorerPresenter.Save();

                        WebClient web = new WebClient();

                        string tempSetupFileName = Path.Combine(Path.GetTempPath(), "APSIMSetup.exe");
                        if (File.Exists(tempSetupFileName))
                        {
                            File.Delete(tempSetupFileName);
                        }

                        try
                        {
                            web.DownloadFile(upgrade.ReleaseURL, tempSetupFileName);
                        }
                        catch (Exception err)
                        {
                            MessageBox.Show("Cannot download this release. Error message is: \r\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        // Write to the registration database.
                        WriteUpgradeRegistration(versionNumber);

                        if (File.Exists(tempSetupFileName))
                        {
                            // Copy the separate upgrader executable to the temp directory.
                            string sourceUpgraderFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Updater.exe");
                            string upgraderFileName       = Path.Combine(Path.GetTempPath(), "Updater.exe");

                            // Delete the old upgrader.
                            if (File.Exists(upgraderFileName))
                            {
                                File.Delete(upgraderFileName);
                            }

                            // Copy in the new upgrader.
                            File.Copy(sourceUpgraderFileName, upgraderFileName, true);

                            // Run the upgrader.
                            string binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            string ourDirectory = Path.GetFullPath(Path.Combine(binDirectory, ".."));
                            string newDirectory = Path.GetFullPath(Path.Combine(ourDirectory, "..", "APSIM" + versionNumber));
                            string arguments    = StringUtilities.DQuote(ourDirectory) + " " +
                                                  StringUtilities.DQuote(newDirectory);

                            ProcessStartInfo info = new ProcessStartInfo();
                            info.FileName         = upgraderFileName;
                            info.Arguments        = arguments;
                            info.WorkingDirectory = Path.GetTempPath();
                            Process.Start(info);

                            Cursor.Current = Cursors.Default;

                            // Shutdown the user interface
                            Close();
                            explorerPresenter.Close();
                        }
                    }
                }
                catch (Exception err)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }