コード例 #1
0
 private void WebDownloader_CallBack(WebDownloadEventArgs wdea)
 {
     if (this.IsHandleCreated)
     {
         this.Invoke(new TS_CallBackDelegate(TS_CallBack), new object[] { wdea });
     }
 }
コード例 #2
0
        private void TS_CallBack(WebDownloadEventArgs wdea)
        {
            switch (wdea.Status)
            {
            case WebDownloadStatus.Downloading:
                pgb.Value = wdea.PercentComplete;
                lblBytesDownloaded.Text = string.Format(Localizer.GetString(this.GetType(),
                                                                            "lblBytesDownloaded.Text"), wdea.BytesDownloaded);
                break;

            case WebDownloadStatus.Fail:
                if (WebDownloader.DownloadException.GetType() == typeof(WebException))
                {
                    MessageBox.Show("Error downloading update from server.");
                }
                else
                {
                    MessageBox.Show("General Error: " + WebDownloader.DownloadException.ToString());
                }
                this.Close();
                break;

            case WebDownloadStatus.Success:
                try
                {
                    string[] deletePaths;
                    using (ZipInputStream zs = new ZipInputStream(WebDownloader.DownloadStream))
                    {
                        ZipEntry ze;

                        while ((ze = zs.GetNextEntry()) != null)
                        {
                            int    size;
                            byte[] data = new byte[2048];
                            string path = Path.Combine(Application.StartupPath, ze.Name);
                            string dir  = Path.GetDirectoryName(path);
                            if (ze.IsDirectory)
                            {
                                continue;
                            }
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }
                            using (FileStream fs = File.Open(path, FileMode.Create))
                            {
                                do
                                {
                                    size = zs.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        fs.Write(data, 0, size);
                                    }
                                } while (size > 0);
                            }
                        }
                    }
                    if (File.Exists(Path.Combine(Application.StartupPath, _deleteFile)))
                    {
                        using (StreamReader sr = new StreamReader(Path.Combine(Application.StartupPath,
                                                                               _deleteFile)))
                            deletePaths = sr.ReadToEnd().Split('|');
                        if (deletePaths != null)
                        {
                            foreach (string deletePath in deletePaths)
                            {
                                if (File.Exists(Path.Combine(Application.StartupPath, deletePath)))
                                {
                                    File.Delete(Path.Combine(Application.StartupPath, deletePath));
                                }
                            }
                        }
                        File.Delete(Path.Combine(Application.StartupPath, _deleteFile));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                this.Close();
                break;
            }
        }