コード例 #1
0
        // Call on MainWindow.Loaded event to check on start-up.
        public static async Task CheckForUpdates(bool Silent)
        {
            if (ServiceURI == null || RemoteFileURI == null)
            {
                throw new Exception("AutoUpdater - RemoteFileURI and ServiceURI must be set.");
            }
            try
            {
                WebClient  webClient  = new WebClient();
                HttpClient httpClient = new HttpClient();
                var        result     = await httpClient.GetAsync(ServiceURI);

                var strServerVersion = await result.Content.ReadAsStringAsync();

                var serverVersion = Version.Parse(strServerVersion);
                var thisVersion   = Application.ResourceAssembly.ManifestModule.Assembly.GetName().Version;
                if (serverVersion > thisVersion)
                {
                    var strFilePath  = Path.GetTempPath() + FileName;
                    var dialogResult = MessageBox.Show("A new version of " + AppName + " is available!  Would you like to download it now?", "New Version Available", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (dialogResult == MessageBoxResult.Yes)
                    {
                        if (File.Exists(strFilePath))
                        {
                            File.Delete(strFilePath);
                        }

                        var windowProgress = new Window();
                        windowProgress.DragMove();
                        windowProgress.Height                = 150;
                        windowProgress.Width                 = 400;
                        windowProgress.WindowStyle           = WindowStyle.None;
                        windowProgress.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                        var progressControl = new DownloadProgressControl();
                        windowProgress.Content             = progressControl;
                        webClient.DownloadProgressChanged += (sender, args) => {
                            progressControl.progressBar.Value = args.ProgressPercentage;
                        };
                        windowProgress.Show();
                        await webClient.DownloadFileTaskAsync(new Uri(RemoteFileURI), strFilePath);

                        windowProgress.Close();

                        var psi = new ProcessStartInfo()
                        {
                            FileName  = strFilePath,
                            Arguments = $"-wpfautoupdate \"${Application.ResourceAssembly.ManifestModule.Assembly.Location}\"",
                            Verb      = "RunAs"
                        };

                        Process.Start(psi);
                        Application.Current.Shutdown();
                        return;
                    }
                }
                else
                {
                    if (!Silent)
                    {
                        MessageBox.Show(AppName + " is up-to-date.", "No Updates", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch
            {
                if (!Silent)
                {
                    MessageBox.Show("Unable to contact the server.  Check your network connection or try again later.", "Server Unreachable", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
                return;
            }
        }
コード例 #2
0
        // Call on MainWindow.Loaded event to check on start-up.
        public static async Task CheckForUpdates(bool Silent)
        {
            if (ServiceURI == null || RemoteFileURI == null)
            {
                throw new Exception("AutoUpdater - RemoteFileURI and ServiceURI must be set.");
            }
            try
            {
                System.Net.WebClient       webClient  = new System.Net.WebClient();
                System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
                var result = await httpClient.GetAsync(ServiceURI);

                var strServerVersion = await result.Content.ReadAsStringAsync();

                var serverVersion = Version.Parse(strServerVersion);
                var thisVersion   = Application.ResourceAssembly.ManifestModule.Assembly.GetName().Version;
                if (serverVersion > thisVersion)
                {
                    var strFilePath  = System.IO.Path.GetTempPath() + FileName;
                    var dialogResult = System.Windows.MessageBox.Show("A new version of " + AppName + " is available!  Would you like to download it now?  It's a no-fuss, instant process.", "New Version Available", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (dialogResult == MessageBoxResult.Yes)
                    {
                        if (System.IO.File.Exists(strFilePath))
                        {
                            System.IO.File.Delete(strFilePath);
                        }
                        var windowProgress = new System.Windows.Window();
                        windowProgress.DragMove();
                        windowProgress.Height                = 150;
                        windowProgress.Width                 = 400;
                        windowProgress.WindowStyle           = WindowStyle.None;
                        windowProgress.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                        var progressControl = new DownloadProgressControl();
                        windowProgress.Content             = progressControl;
                        webClient.DownloadProgressChanged += (sender, args) => {
                            progressControl.progressBar.Value = args.ProgressPercentage;
                        };
                        windowProgress.Show();
                        await webClient.DownloadFileTaskAsync(new Uri(RemoteFileURI), strFilePath);

                        windowProgress.Close();
                        var psi = new ProcessStartInfo(strFilePath, "-wpfautoupdate \"" + Application.ResourceAssembly.ManifestModule.Assembly.Location + "\"");

                        // Check if target directory is writable with current privileges.  If not, start update process as admin.
                        try
                        {
                            var installPath = Path.GetDirectoryName(Application.ResourceAssembly.ManifestModule.Assembly.Location);
                            var fi          = new FileInfo(Path.Combine(installPath, "Test.txt"));
                            fi.Create();
                            fi.Delete();
                        }
                        catch
                        {
                            psi.Verb = "runas";
                        }
                        Process.Start(psi);
                        Application.Current.Shutdown();
                        return;
                    }
                }
                else
                {
                    if (!Silent)
                    {
                        MessageBox.Show(AppName + " is up-to-date.", "No Updates", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch
            {
                if (!Silent)
                {
                    MessageBox.Show("Unable to contact the server.  Check your network connection or try again later.", "Server Unreachable", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
                return;
            }
        }