Esempio n. 1
0
        void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            HttpWebRequest  req         = null;
            HttpWebResponse res         = null;
            Stream          resStream   = null;
            Stream          localStream = null;

            try
            {
                try
                {
                    req = (HttpWebRequest)WebRequest.Create(BlazeWebInfo.GetBlazeDownloadUrl());
                }
                catch
                {
                    MessageBox.Show("The server could not be reached. Please try again later.", "Blaze Updater", MessageBoxButton.OK, MessageBoxImage.Error);
                    _backgroundWorker.CancelAsync();
                }
                req.ContentType = "Application";
                req.Credentials = CredentialCache.DefaultCredentials;
                res             = (HttpWebResponse)req.GetResponse();

                Int64 fileSize = res.ContentLength;
                _fileSize = Math.Ceiling((double)(fileSize) / (double)1024);

                string filename = res.Headers.Get("Content-Disposition");

                resStream = res.GetResponseStream();

                localStream = new FileStream(CommonInfo.BlazeTempPath, FileMode.Create, FileAccess.Write, FileShare.None);

                int bytesSize = 0;

                byte[] downBuffer = new byte[2048];

                _beginning = DateTime.Now;
                while ((bytesSize = resStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    localStream.Write(downBuffer, 0, bytesSize);

                    _backgroundWorker.ReportProgress(Convert.ToInt32((localStream.Length * 100) / fileSize));

                    if (_backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("A error ocurred while downloading Blaze:" + Environment.NewLine + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine + "Please try again later.", "Blaze Updater", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                if (req != null)
                {
                    req = null;
                }
                if (res != null)
                {
                    res.Close();
                }
                if (resStream != null)
                {
                    resStream.Close();
                }
                if (localStream != null)
                {
                    localStream.Close();
                }
            }
        }
Esempio n. 2
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            bool test = false;

            try
            {
                test = mutex.WaitOne(TimeSpan.Zero, true);
            }
            catch
            {
                test = true;
            }
            if (test)
            {
                _is_safe = true;
                if (e.Args != null && e.Args.Count() > 0 && e.Args[0] == "-suppress")
                {
                    _suppress = true;
                }
                Version version_on_server = null;
                try
                {
                    version_on_server = BlazeWebInfo.GetBlazeVersion();
                }
                catch
                {
                    if (!_suppress)
                    {
                        MessageBox.Show("The server could not be reached. Please try again later.", "Blaze Updater", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    this.Shutdown();
                }
                AssemblyName assemblyName  = AssemblyName.GetAssemblyName(CommonInfo.BlazeExePath);
                Version      local_version = assemblyName.Version;

                if (version_on_server > local_version)
                {
                    if (MessageBox.Show("There is a new version of Blaze available. Would you like to download it now?", "Blaze Updater", MessageBoxButton.YesNo, MessageBoxImage.Question)
                        == MessageBoxResult.No)
                    {
                        this.Shutdown();
                    }
                    else
                    {
                        this.StartupUri = new Uri("Window1.xaml", UriKind.Relative);
                    }
                }
                else
                {
                    if (!_suppress)
                    {
                        MessageBox.Show("Blaze is up to date. No update is required.", "Blaze Updater", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    this.Shutdown();
                }
            }
            else
            {
                this.Shutdown();
            }
        }