using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using UpdateLib.Data; namespace UpdateLib.UI { public partial class frmUpdater : Form { int index = 0; private UpdateSaveFile localInfoFile; private String baseUrl; public frmUpdater(UpdateSaveFile file, String baseUrl) { InitializeComponent(); localInfoFile = file; pbInstall.Maximum = (file.UpdateFileCollection.Count) * 100; this.baseUrl = baseUrl; foreach (UpdateFileInfo fileInfo in file.UpdateFileCollection) { ListViewItem lvItem = new ListViewItem(new String[] { fileInfo.Name, "Waiting...", fileInfo.Description }); lvItems.Items.Add(lvItem); } } private void btnInstall_Click(object sender, EventArgs e) { if (btnInstall.Text.Equals("Finish")) { MessageBox.Show("Update erfolgreich. Programm wird beendet"); Application.Exit(); } else { btnInstall.Enabled = false; DownloadFile(); } } private void DownloadFile() { WebClient downloadClient = new WebClient(); downloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadClient_DownloadProgressChanged); downloadClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadClient_DownloadFileCompleted); ListViewItem currItem = lvItems.Items[index]; String name = currItem.SubItems[0].Text; SetStatus(String.Format("Downloading {0} ...", name)); currItem.SubItems[1].Text = "Downloading..."; String local = String.Format(@".\{0}", name); String online = String.Format("{0}{1}", baseUrl, name); if (File.Exists(local)) { if (File.Exists(local + ".old")) File.Delete(local + ".old"); File.Move(local, local + ".old"); } downloadClient.DownloadFileAsync(new Uri(online), local); } private void SetStatus(string p) { lblStatus.Text = String.Format("Status: {0}", p); } private void downloadClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { ListViewItem currItem = lvItems.Items[index]; currItem.SubItems[1].Text = "Downloaded"; pbInstall.Increment(100); pbDownload.Value = 0; index++; if (lvItems.Items.Count - 1 >= index) DownloadFile(); else { SetStatus("Finished!"); btnInstall.Text = "Finish"; btnInstall.Enabled = true; } } private void downloadClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { pbDownload.Value = e.ProgressPercentage; } } }