public void PropertyChangedHandler(object sender, PropertyChangedEventArgs e) { WebDownloadClient download = (WebDownloadClient)sender; if (e.PropertyName == "AverageSpeedAndTotalTime" && download.Status != DownloadStatus.Deleting) { this.Dispatcher.Invoke(new PropertyChangedEventHandler(UpdatePropertiesList), sender, e); } }
public void DownloadCompletedHandler(object sender, EventArgs e) { if (Settings.Default.ShowBalloonNotification) { WebDownloadClient download = (WebDownloadClient)sender; if (download.Status == DownloadStatus.Completed) { string title = "Загрузка завершена"; string text = download.FileName + " окончил загрузку."; XNotifyIcon.ShowBalloonTip(title, text, BalloonIcon.Info); } } }
public void DownloadCompletedHandler(object sender, EventArgs e) { if (Settings.Default.ShowBalloonNotification) { WebDownloadClient download = (WebDownloadClient)sender; if (download.Status == DownloadStatus.Completed) { string title = "Download Completed"; string text = download.FileName + " has finished downloading."; XNotifyIcon.ShowBalloonTip(title, text, BalloonIcon.Info); } } }
private void btnUpdateSelected_Click(object sender, RoutedEventArgs e) { if (updateGrid.SelectedItems.Count > 0) { var selectedUpdate = updateGrid.SelectedItems.Cast <ProgVersion>(); foreach (ProgVersion updatable in selectedUpdate) { if (IsUrlValid(updatable.Url.Trim())) { try { WebDownloadClient download = new WebDownloadClient(updatable.Url); string updateablefile = updatable.Name; updateablefile = updateablefile + ".exe"; download.FileName = updateablefile; download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += this.mainWindow.PropertyChangedHandler; download.StatusChanged += this.mainWindow.StatusChangedHandler; download.DownloadCompleted += this.mainWindow.DownloadCompletedHandler; if (!Directory.Exists(Settings.Default.DownloadLocation)) { Directory.CreateDirectory(Settings.Default.DownloadLocation); } string filePath = Settings.Default.DownloadLocation + download.FileName; string tempPath = filePath + ".tmp"; if (File.Exists(tempPath)) { string message = "Уже выполняется загрузка по указнному пути."; Xceed.Wpf.Toolkit.MessageBox.Show(message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (File.Exists(filePath)) { File.Delete(filePath); } download.CheckUrl(); if (download.HasError) { return; } download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; download.OpenFileOnCompletion = true; DownloadManager.Instance.DownloadsList.Add(download); download.Start(); this.Close(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Xceed.Wpf.Toolkit.MessageBox.Show("Указанный в файле путь загрузки файла обновления недействителен", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } }
public void Checker() { if (IsUrlValid(tbServerAddr.Text.Trim())) { string version = null; string url = null; VersionChecker verChecker = new VersionChecker(); WebDownloadClient download = new WebDownloadClient(tbServerAddr.Text.Trim()); download.CheckUrl(); if (download.HasError) { return; } try { HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(tbServerAddr.Text.Trim()); HttpWebResponse response = (HttpWebResponse)rq.GetResponse(); string addr = tbServerAddr.Text.Trim(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { addr = reader.ReadToEnd(); } XmlDocument doc = new XmlDocument(); doc.LoadXml(addr); XmlElement xdoc = doc.DocumentElement; foreach (XmlNode node in xdoc) { string name = node.Attributes[0].Value; foreach (XmlNode childnode in node.ChildNodes) { if (childnode.Name == "Version") { version = childnode.InnerText; } if (childnode.Name == "Url") { url = childnode.InnerText; } } upd.Add(new ProgName(name, version, url)); } } catch (Exception ss) { string message = ss.ToString(); MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "SecretService"); } string sb1 = null; string sb2 = null; RegistryKey key; key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); foreach (String keyName in key.GetSubKeyNames()) { try { sb1 = null; sb2 = null; RegistryKey subkey = key.OpenSubKey(keyName); sb1 = subkey.GetValue("DisplayName") as string; sb2 = subkey.GetValue("DisplayVersion") as string; var outcomeupd = upd.FindIndex(x => string.Equals(x.Name, sb1, StringComparison.CurrentCultureIgnoreCase)); if (outcomeupd != -1) { string vers = upd[outcomeupd].Version; string realurl = upd[outcomeupd].Url; if (verChecker.NewVersionExists(sb2, vers)) { if (upd.Exists(x => x.Name == sb1)) { outcome.Add(new ProgVersion(sb1, sb2, vers, realurl)); } } } else { continue; } } catch (Exception ss) { string message = ss.ToString(); MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "SecretService"); } } key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); foreach (String keyName in key.GetSubKeyNames()) { try { sb1 = null; sb2 = null; RegistryKey subkey = key.OpenSubKey(keyName); sb1 = subkey.GetValue("DisplayName") as string; sb2 = subkey.GetValue("DisplayVersion") as string; var outcomeupd = upd.FindIndex(x => string.Equals(x.Name, sb1, StringComparison.CurrentCultureIgnoreCase)); if (outcomeupd != -1) { string vers = upd[outcomeupd].Version; string realurl = upd[outcomeupd].Url; if (verChecker.NewVersionExists(sb2, vers)) { if (upd.Exists(x => x.Name == sb1)) { outcome.Add(new ProgVersion(sb1, sb2, vers, realurl)); } } } else { continue; } } catch (Exception ss) { string message = ss.ToString(); MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "SecretService"); } } } else { Xceed.Wpf.Toolkit.MessageBox.Show("Указанный URL недействителен.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnDownload_Click(object sender, RoutedEventArgs e) { if (urlValid) { if (tbSaveAs.Text.Length < 3 || !tbSaveAs.Text.Contains(".")) { Xceed.Wpf.Toolkit.MessageBox.Show("The local file name is not valid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } try { WebDownloadClient download = new WebDownloadClient(tbURL.Text.Trim()); download.FileName = tbSaveAs.Text.Trim(); // Register WebDownloadClient events download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += this.mainWindow.PropertyChangedHandler; download.StatusChanged += this.mainWindow.StatusChangedHandler; download.DownloadCompleted += this.mainWindow.DownloadCompletedHandler; // Create path to temporary file if (!Directory.Exists(tbDownloadFolder.Text)) { Directory.CreateDirectory(tbDownloadFolder.Text); } string filePath = tbDownloadFolder.Text + download.FileName; string tempPath = filePath + ".tmp"; // Check if there is already an ongoing download on that path if (File.Exists(tempPath)) { string message = "There is already a download in progress at the specified path."; Xceed.Wpf.Toolkit.MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // Check if the file already exists if (File.Exists(filePath)) { string message = "There is already a file with the same name, do you want to overwrite it? " + "If not, please change the file name or download folder."; MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "File Name Conflict: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { File.Delete(filePath); } else return; } // Set username and password if HTTP authentication is required if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0)) { download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim()); } // Check the URL download.CheckUrl(); if (download.HasError) return; download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; download.OpenFileOnCompletion = this.cbOpenFileOnCompletion.IsChecked.Value; // Add the download to the downloads list DownloadManager.Instance.DownloadsList.Add(download); // Start downloading the file if (startImmediately) download.Start(); else download.Status = DownloadStatus.Paused; // Close the Add New Download window this.Close(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Xceed.Wpf.Toolkit.MessageBox.Show("The URL is not a valid download link.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnDownload_Click(object sender, RoutedEventArgs e) { if (urlValid) { if (tbSaveAs.Text.Length < 3 || !tbSaveAs.Text.Contains(".")) { Xceed.Wpf.Toolkit.MessageBox.Show("The local file name is not valid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } try { WebDownloadClient download = new WebDownloadClient(tbURL.Text.Trim()); download.FileName = tbSaveAs.Text.Trim(); // Register WebDownloadClient events download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += this.mainWindow.PropertyChangedHandler; download.StatusChanged += this.mainWindow.StatusChangedHandler; download.DownloadCompleted += this.mainWindow.DownloadCompletedHandler; // Create path to temporary file if (!Directory.Exists(tbDownloadFolder.Text)) { Directory.CreateDirectory(tbDownloadFolder.Text); } string filePath = tbDownloadFolder.Text + download.FileName; string tempPath = filePath + ".tmp"; // Check if there is already an ongoing download on that path if (File.Exists(tempPath)) { string message = "There is already a download in progress at the specified path."; Xceed.Wpf.Toolkit.MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // Check if the file already exists if (File.Exists(filePath)) { string message = "There is already a file with the same name, do you want to overwrite it? " + "If not, please change the file name or download folder."; MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "File Name Conflict: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { File.Delete(filePath); } else { return; } } // Set username and password if HTTP authentication is required if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0)) { download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim()); } // Check the URL download.CheckUrl(); if (download.HasError) { return; } download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; download.OpenFileOnCompletion = this.cbOpenFileOnCompletion.IsChecked.Value; // Add the download to the downloads list DownloadManager.Instance.DownloadsList.Add(download); // Start downloading the file if (startImmediately) { download.Start(); } else { download.Status = DownloadStatus.Paused; } // Close the Add New Download window this.Close(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Xceed.Wpf.Toolkit.MessageBox.Show("The URL is not a valid download link.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void StatusChanged(object sender, EventArgs e) { // Start the first download in the queue, if it exists WebDownloadClient dl = (WebDownloadClient)sender; if (dl.Status == DownloadStatus.Paused || dl.Status == DownloadStatus.Completed || dl.Status == DownloadStatus.Deleted || dl.HasError) { foreach (WebDownloadClient d in DownloadManager.Instance.DownloadsList) { if (d.Status == DownloadStatus.Queued) { d.Start(); break; } } } foreach (WebDownloadClient d in DownloadManager.Instance.DownloadsList) { if (d.Status == DownloadStatus.Downloading) { d.SpeedLimitChanged = true; } } int active = DownloadManager.Instance.ActiveDownloads; int completed = DownloadManager.Instance.CompletedDownloads; if (active > 0) { if (completed == 0) { this.statusBarActive.Content = " (" + active + " Активно)"; } else { this.statusBarActive.Content = " (" + active + " Активно, "; } } else { this.statusBarActive.Content = String.Empty; } if (completed > 0) { if (active == 0) { this.statusBarCompleted.Content = " (" + completed + " Завершено)"; } else { this.statusBarCompleted.Content = completed + " Завершено)"; } } else { this.statusBarCompleted.Content = String.Empty; } }
private void LoadDownloadsFromXml() { try { if (File.Exists("Downloads.xml")) { // Load downloads from XML file XElement downloads = XElement.Load("Downloads.xml"); if (downloads.HasElements) { IEnumerable <XElement> downloadsList = from el in downloads.Elements() select el; foreach (XElement download in downloadsList) { // Create WebDownloadClient object based on XML data WebDownloadClient downloadClient = new WebDownloadClient(download.Element("url").Value); downloadClient.FileName = download.Element("file_name").Value; downloadClient.DownloadProgressChanged += downloadClient.DownloadProgressChangedHandler; downloadClient.DownloadCompleted += downloadClient.DownloadCompletedHandler; downloadClient.PropertyChanged += this.PropertyChangedHandler; downloadClient.StatusChanged += this.StatusChangedHandler; downloadClient.DownloadCompleted += this.DownloadCompletedHandler; string username = download.Element("username").Value; string password = download.Element("password").Value; if (username != String.Empty && password != String.Empty) { downloadClient.ServerLogin = new NetworkCredential(username, password); } downloadClient.TempDownloadPath = download.Element("temp_path").Value; downloadClient.FileSize = Convert.ToInt64(download.Element("file_size").Value); downloadClient.DownloadedSize = Convert.ToInt64(download.Element("downloaded_size").Value); DownloadManager.Instance.DownloadsList.Add(downloadClient); if (download.Element("status").Value == "Completed") { downloadClient.Status = DownloadStatus.Completed; } else { downloadClient.Status = DownloadStatus.Paused; } downloadClient.StatusText = download.Element("status_text").Value; downloadClient.ElapsedTime = TimeSpan.Parse(download.Element("total_time").Value); downloadClient.AddedOn = DateTime.Parse(download.Element("added_on").Value); downloadClient.CompletedOn = DateTime.Parse(download.Element("completed_on").Value); downloadClient.SupportsRange = Boolean.Parse(download.Element("supports_resume").Value); downloadClient.HasError = Boolean.Parse(download.Element("has_error").Value); downloadClient.OpenFileOnCompletion = Boolean.Parse(download.Element("open_file").Value); downloadClient.TempFileCreated = Boolean.Parse(download.Element("temp_created").Value); downloadClient.IsBatch = Boolean.Parse(download.Element("is_batch").Value); downloadClient.BatchUrlChecked = Boolean.Parse(download.Element("url_checked").Value); if (downloadClient.Status == DownloadStatus.Paused && !downloadClient.HasError && Settings.Default.StartDownloadsOnStartup) { downloadClient.Start(); } } // Create empty XML file XElement root = new XElement("downloads"); XDocument xd = new XDocument(); xd.Add(root); xd.Save("Downloads.xml"); } } } catch (Exception) { Xceed.Wpf.Toolkit.MessageBox.Show("Ошибка при загрузке листа загрузок", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnDownload_Click(object sender, RoutedEventArgs e) { if (downloadUrls.Count > 0) { try { foreach (string url in downloadUrls) { WebDownloadClient download = new WebDownloadClient(url); download.IsBatch = true; download.BatchUrlChecked = false; download.FileName = url.Substring(url.LastIndexOf('/') + 1); // Register WebDownloadClient events download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += this.mainWindow.PropertyChangedHandler; download.StatusChanged += this.mainWindow.StatusChangedHandler; download.DownloadCompleted += this.mainWindow.DownloadCompletedHandler; // Create path to temporary file if (!Directory.Exists(tbDownloadFolder.Text)) { Directory.CreateDirectory(tbDownloadFolder.Text); } string filePath = tbDownloadFolder.Text + download.FileName; string tempPath = filePath + ".tmp"; // Check if there is already an ongoing download on that path if (File.Exists(tempPath)) { // If there is, skip adding this download continue; } // Check if the file already exists if (File.Exists(filePath)) { string message = "Такой файл уже существует. Перезаписать?"; MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "Конфликт имен файлов: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { File.Delete(filePath); } else { // Skip adding this download continue; } } // Set username and password if HTTP authentication is required if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0)) { download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim()); } download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; // Add the download to the downloads list DownloadManager.Instance.DownloadsList.Add(download); // Start downloading the file if (startImmediately) { download.Start(); } else { download.Status = DownloadStatus.Paused; } } // Close the Create Batch Download window this.Close(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Xceed.Wpf.Toolkit.MessageBox.Show("Нет файлов для загрузки!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnDownload_Click(object sender, RoutedEventArgs e) { if (downloadUrls.Count > 0) { try { foreach (string url in downloadUrls) { WebDownloadClient download = new WebDownloadClient(url); download.IsBatch = true; download.BatchUrlChecked = false; download.FileName = url.Substring(url.LastIndexOf('/') + 1); // Register WebDownloadClient events download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += this.mainWindow.PropertyChangedHandler; download.StatusChanged += this.mainWindow.StatusChangedHandler; download.DownloadCompleted += this.mainWindow.DownloadCompletedHandler; // Create path to temporary file if (!Directory.Exists(tbDownloadFolder.Text)) { Directory.CreateDirectory(tbDownloadFolder.Text); } string filePath = tbDownloadFolder.Text + download.FileName; string tempPath = filePath + ".tmp"; // Check if there is already an ongoing download on that path if (File.Exists(tempPath)) { // If there is, skip adding this download continue; } // Check if the file already exists if (File.Exists(filePath)) { string message = "There is already a file with the same name, do you want to overwrite it?"; MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "File Name Conflict: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { File.Delete(filePath); } else { // Skip adding this download continue; } } // Set username and password if HTTP authentication is required if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0)) { download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim()); } download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; // Add the download to the downloads list DownloadManager.Instance.DownloadsList.Add(download); // Start downloading the file if (startImmediately) download.Start(); else download.Status = DownloadStatus.Paused; } // Close the Create Batch Download window this.Close(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else Xceed.Wpf.Toolkit.MessageBox.Show("There are no files to download!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); }
private void LoadDownloadsFromXml() { try { if (File.Exists("Downloads.xml")) { // Load downloads from XML file XElement downloads = XElement.Load("Downloads.xml"); if (downloads.HasElements) { IEnumerable<XElement> downloadsList = from el in downloads.Elements() select el; foreach (XElement download in downloadsList) { // Create WebDownloadClient object based on XML data WebDownloadClient downloadClient = new WebDownloadClient(download.Element("url").Value); downloadClient.FileName = download.Element("file_name").Value; downloadClient.DownloadProgressChanged += downloadClient.DownloadProgressChangedHandler; downloadClient.DownloadCompleted += downloadClient.DownloadCompletedHandler; downloadClient.PropertyChanged += this.PropertyChangedHandler; downloadClient.StatusChanged += this.StatusChangedHandler; downloadClient.DownloadCompleted += this.DownloadCompletedHandler; string username = download.Element("username").Value; string password = download.Element("password").Value; if (username != String.Empty && password != String.Empty) { downloadClient.ServerLogin = new NetworkCredential(username, password); } downloadClient.TempDownloadPath = download.Element("temp_path").Value; downloadClient.FileSize = Convert.ToInt64(download.Element("file_size").Value); downloadClient.DownloadedSize = Convert.ToInt64(download.Element("downloaded_size").Value); DownloadManager.Instance.DownloadsList.Add(downloadClient); if (download.Element("status").Value == "Completed") { downloadClient.Status = DownloadStatus.Completed; } else { downloadClient.Status = DownloadStatus.Paused; } downloadClient.StatusText = download.Element("status_text").Value; downloadClient.ElapsedTime = TimeSpan.Parse(download.Element("total_time").Value); downloadClient.AddedOn = DateTime.Parse(download.Element("added_on").Value); downloadClient.CompletedOn = DateTime.Parse(download.Element("completed_on").Value); downloadClient.SupportsRange = Boolean.Parse(download.Element("supports_resume").Value); downloadClient.HasError = Boolean.Parse(download.Element("has_error").Value); downloadClient.OpenFileOnCompletion = Boolean.Parse(download.Element("open_file").Value); downloadClient.TempFileCreated = Boolean.Parse(download.Element("temp_created").Value); downloadClient.IsBatch = Boolean.Parse(download.Element("is_batch").Value); downloadClient.BatchUrlChecked = Boolean.Parse(download.Element("url_checked").Value); if (downloadClient.Status == DownloadStatus.Paused && !downloadClient.HasError && Settings.Default.StartDownloadsOnStartup) { downloadClient.Start(); } } // Create empty XML file XElement root = new XElement("downloads"); XDocument xd = new XDocument(); xd.Add(root); xd.Save("Downloads.xml"); } } } catch (Exception) { Xceed.Wpf.Toolkit.MessageBox.Show("There was an error while loading the download list.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnDownload_Click(object sender, RoutedEventArgs e) { if (urlValid) { if (tbSaveAs.Text.Length < 3 || !tbSaveAs.Text.Contains(".")) { Xceed.Wpf.Toolkit.MessageBox.Show("Имя файла неверно.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); return; } try { WebDownloadClient download = new WebDownloadClient(tbURL.Text.Trim()); download.FileName = tbSaveAs.Text.Trim(); // Register WebDownloadClient events download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += this.mainWindow.PropertyChangedHandler; download.StatusChanged += this.mainWindow.StatusChangedHandler; download.DownloadCompleted += this.mainWindow.DownloadCompletedHandler; // Create path to temporary file if (!Directory.Exists(tbDownloadFolder.Text)) { Directory.CreateDirectory(tbDownloadFolder.Text); } string filePath = tbDownloadFolder.Text + download.FileName; string tempPath = filePath + ".tmp"; // Check if there is already an ongoing download on that path if (File.Exists(tempPath)) { string message = "Уже выполняется загрузка по указнному пути."; Xceed.Wpf.Toolkit.MessageBox.Show(message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); return; } // Check if the file already exists if (File.Exists(filePath)) { string message = "Файл с таким именем уже сущестует. Перезаписать? " + "Если нет, пожалуйста измените название файла или путь загрузки."; MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "Конфликт имен файлов: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { File.Delete(filePath); } else { return; } } // Set username and password if HTTP authentication is required if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0)) { download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim()); } // Check the URL download.CheckUrl(); if (download.HasError) { return; } download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; download.OpenFileOnCompletion = this.cbOpenFileOnCompletion.IsChecked.Value; // Add the download to the downloads list DownloadManager.Instance.DownloadsList.Add(download); // Start downloading the file if (startImmediately) { download.Start(); } else { download.Status = DownloadStatus.Paused; } // Close the Add New Download window this.Close(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Xceed.Wpf.Toolkit.MessageBox.Show("Указанный URL недействителен.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void Donwload_and_install(string url, string path) { if (IsUrlValid(url)) { try { WebDownloadClient download = new WebDownloadClient(url); string updateablefile = url.Substring(url.LastIndexOf("/") + 1); if (path != null) { download.execpath = @path; } download.FileName = updateablefile; download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += this.mainWindow.PropertyChangedHandler; download.StatusChanged += this.mainWindow.StatusChangedHandler; download.DownloadCompleted += this.mainWindow.DownloadCompletedHandler; if (!Directory.Exists(Settings.Default.DownloadLocation)) { Directory.CreateDirectory(Settings.Default.DownloadLocation); } string filePath = Settings.Default.DownloadLocation + download.FileName; string tempPath = filePath + ".tmp"; if (File.Exists(tempPath)) { string message = "Уже выполняется загрузка по указнному пути."; Xceed.Wpf.Toolkit.MessageBox.Show(message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (File.Exists(filePath)) { File.Delete(filePath); } download.CheckUrl(); if (download.HasError) { return; } download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; download.OpenFileOnCompletion = true; DownloadManager.Instance.DownloadsList.Add(download); download.Start(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Xceed.Wpf.Toolkit.MessageBox.Show("Указанный в файле путь загрузки файла обновления недействителен", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } }
void btnDownload_Click(object sender, RoutedEventArgs e) { if (downloadUrls.Count > 0) { try { foreach (string url in downloadUrls) { var download = new WebDownloadClient(url) { IsBatch = true, BatchUrlChecked = false, FileName = url.Substring(url.LastIndexOf('/') + 1) }; // Register WebDownloadClient events download.DownloadProgressChanged += download.DownloadProgressChangedHandler; download.DownloadCompleted += download.DownloadCompletedHandler; download.PropertyChanged += mainWindow.PropertyChangedHandler; download.StatusChanged += mainWindow.StatusChangedHandler; download.DownloadCompleted += mainWindow.DownloadCompletedHandler; // Create path to temporary file if (!Directory.Exists(tbDownloadFolder.Text)) { Directory.CreateDirectory(tbDownloadFolder.Text); } string filePath = Path.Combine(tbDownloadFolder.Text, download.FileName); string tempPath = filePath + ".tmp"; // Check if there is already an ongoing download on that path if (File.Exists(tempPath)) { // If there is, skip adding this download continue; } // Check if the file already exists if (File.Exists(filePath)) { string message = "There is already a file with the same name, do you want to overwrite it?"; MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "File Name Conflict: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { File.Delete(filePath); } else { // Skip adding this download continue; } } // Set username and password if HTTP authentication is required if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0)) { download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim()); } download.TempDownloadPath = tempPath; download.AddedOn = DateTime.UtcNow; download.CompletedOn = DateTime.MinValue; // Add the download to the downloads list DownloadManager.Instance.DownloadsList.Add(download); // Start downloading the file if (startImmediately) { download.Start(); } else { download.Status = DownloadStatus.Paused; } } // Close the Create Batch Download window Close(); } catch (Exception ex) { Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { Xceed.Wpf.Toolkit.MessageBox.Show("There are no files to download!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }