private async void btnUploadComputer_Click(object sender, RoutedEventArgs e) { spUploadSettings.IsEnabled = false; btnScrap.IsEnabled = false; DownloadImageList.IsEnabled = false; spprewController.IsEnabled = false; try { var openFileDialog = new System.Windows.Forms.FolderBrowserDialog(); openFileDialog.ShowNewFolderButton = true; openFileDialog.SelectedPath = _downloadpath; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { _downloadpath = Path.Combine(openFileDialog.SelectedPath, _imageDirectory); if (!Directory.Exists(_downloadpath)) { Directory.CreateDirectory(_downloadpath); } Properties.Settings.Default.ScrapDownloadPath = openFileDialog.SelectedPath; Properties.Settings.Default.Save(); var c = spUploadSettings.Children.OfType <RadioButton>().FirstOrDefault(r => r.GroupName == "grpSize" && r.IsChecked.Value)?.Tag?.ToString() ?? "orginal"; var pathName = c == "custom" ? "orginal" : c; var path = TheGamesDbHandler.BaseUrls.FirstOrDefault(s => s.Name == pathName)?.Path; if (path == null) { return; } var list = _vieModel.DownloadList.Where(l => l.IsSelected); var count = 0; pbUpload.Maximum = list.Count(); pbUpload2.Value = 0; var root = new RetroGameHandler.TimeOnline.root(); var pathGameList = Path.Combine(openFileDialog.SelectedPath, "gamelist.xml"); if (!cbClearGameList.IsChecked.Value && File.Exists(pathGameList)) { using (var ms = new MemoryStream(File.ReadAllBytes(pathGameList))) { root = RetroGameHandler.TimeOnline.root.LoadGamelist(ms); } } foreach (var item in list.ToList()) { var game = root.Games.FirstOrDefault(g => g.Path == $"./{Path.GetFileName(item.FullName)}"); if (game == null) { game = new game(item); root.Games.Add(game); } game.Path = $"./{Path.GetFileName(item.FullName)}"; game.ImagePath = $"./{_imageDirectory}/{Path.GetFileNameWithoutExtension(item.FullName)}{Path.GetExtension(item.DownloadPath)}"; game.players = item.Players.ToString(); game.Publisher = item.Publisher; game.Rating = item.Rating; var gamename = Path.GetFileNameWithoutExtension(item.FullName); var newFilepath = Path.Combine(_downloadpath, gamename); newFilepath += Path.GetExtension(item.DownloadPath); await Dispatcher.BeginInvoke((Action)(() => { pbUpload.Value = count; pbUploadStaus.Text = newFilepath; })); count++; if (File.Exists(newFilepath)) { File.Delete(newFilepath); } var Url = path + item.DownloadPath; byte[] ImageStream; await Task.Run(() => { using (WebClient client = new WebClient()) { ImageStream = client.DownloadData(Url); } using (MemoryStream mem = new MemoryStream(ImageStream)) { WriteToFile(mem, newFilepath); } }); } ProcessStartInfo startInfo = new ProcessStartInfo { Arguments = openFileDialog.SelectedPath, FileName = "explorer.exe" }; root.save(pathGameList); Process.Start(startInfo); } } catch (Exception) { throw; } finally { pbUpload.Value = 0; pbUploadStaus.Text = ""; spUploadSettings.IsEnabled = true; btnScrap.IsEnabled = true; DownloadImageList.IsEnabled = true; spprewController.IsEnabled = true; } }
private async void btnUploadToConsole_Click(object sender, RoutedEventArgs e) { spUploadSettings.IsEnabled = false; btnScrap.IsEnabled = false; DownloadImageList.IsEnabled = false; spprewController.IsEnabled = false; _vieModel.FtpHelper.PropertyChanged += (s, ea) => { Dispatcher.BeginInvoke((Action)(() => { pbUpload.Value = _vieModel.FtpHelper.Progress; pbUploadStaus.Text = _vieModel.FtpHelper.TransMessage; })); }; var c = spUploadSettings.Children.OfType <RadioButton>().FirstOrDefault(r => r.GroupName == "grpSize" && r.IsChecked.Value)?.Tag?.ToString() ?? "orginal"; var pathName = c == "custom" ? "orginal" : c; var path = TheGamesDbHandler.BaseUrls.FirstOrDefault(s => s.Name == pathName)?.Path; if (path == null) { return; } var list = _vieModel.DownloadList.Where(l => l.IsSelected); var count = 0; pbUpload2.Maximum = list.Count(); var root = new RetroGameHandler.TimeOnline.root(); if (!cbClearGameList.IsChecked.Value && await _vieModel.FtpHelper.FieExist($@"{_vieModel.FtpListItem.FullName }/gamelist.xml")) { using (var ms = new MemoryStream()) { if (_vieModel.FtpHelper.DownloadStream($@"{_vieModel.FtpListItem.FullName }/gamelist.xml", ms)) { root = RetroGameHandler.TimeOnline.root.LoadGamelist(ms); } } } foreach (var item in list.ToList()) { var game = root.Games.FirstOrDefault(g => g.Path == $"./{Path.GetFileName(item.FullName)}"); if (game == null) { game = new game(item); root.Games.Add(game); } game.Path = $"./{Path.GetFileName(item.FullName)}"; game.ImagePath = $"./{_imageDirectory}/{Path.GetFileNameWithoutExtension(item.FullName)}{Path.GetExtension(item.DownloadPath)}"; game.players = item.Players.ToString(); game.Publisher = item.Publisher; game.Rating = item.Rating; count++; uploadStatus.Text = $"{count} / {pbUpload2.Maximum}"; pbUpload2.Value = count; var Url = path + item.DownloadPath; byte[] ImageStream; await Task.Run(async() => { using (WebClient client = new WebClient()) { ImageStream = client.DownloadData(Url); } using (MemoryStream mem = new MemoryStream(ImageStream)) { var name = Path.GetFileNameWithoutExtension(item.FullName) + ".png"; var filePathConsole = $@"{_vieModel.FtpListItem.FullName }/{_imageDirectory}/{name}"; var isok = await _vieModel.FtpHelper.UploadStreamAsync(mem, filePathConsole); if (isok) { await Task.Run(() => { IReadOnlyList <DownloadImageModel> itemToRemove = _vieModel.DownloadList.Where(x => (x.Name == item.Name)). ToList(); foreach (var itm in itemToRemove) { _vieModel.DownloadList.Remove(itm); } }); } else { return; } } }); } if (cbCreateGameList.IsChecked.Value) { var xmlStr = root.ToString(); byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(xmlStr); using (MemoryStream mem = new MemoryStream(byteArray)) { var filePathConsole = $@"{_vieModel.FtpListItem.FullName }/gamelist.xml"; var isok = await _vieModel.FtpHelper.UploadStreamAsync(mem, filePathConsole); } } await updateDownloadImageList(true, await UpdateChar()); spUploadSettings.IsEnabled = true; btnScrap.IsEnabled = true; DownloadImageList.IsEnabled = true; spprewController.IsEnabled = true; //sbUpload }