예제 #1
0
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            DownloadProduct downloadProduct = (DownloadProduct)e.Argument;
            var             worker          = sender as BackgroundWorker;

            worker.ReportProgress(0, String.Format("Processing Iteration 1"));

            DownloadServiceClient download = new DownloadServiceClient("NetTcpBinding_IDownloadService");
            long DownloadSize;

            using (FileStream outputStream = new FileStream($@"{downloadProduct.path}.zip", FileMode.OpenOrCreate, FileAccess.Write))
            {
                if (File.Exists($@"{downloadProduct.path}.zip"))
                {
                    FileInfo file = new FileInfo($@"{downloadProduct.path}.zip");
                    DownloadSize          = file.Length;
                    outputStream.Position = DownloadSize;
                }
                else
                {
                    DownloadSize = 0;
                }

                long      FileSize   = download.GetFileSize(downloadProduct.idGame);
                const int bufferSize = 16 * 1024;

                byte[] buffer = new byte[bufferSize];
                using (Stream stream = download.DownloadProduct(downloadProduct.idGame, downloadProduct.idUser, downloadProduct.path, DownloadSize))
                {
                    int bytesRead = stream.Read(buffer, 0, bufferSize);
                    while (bytesRead > 0)
                    {
                        FileInfo file = new FileInfo($@"{downloadProduct.path}.zip");
                        DownloadSize = file.Length;
                        worker.ReportProgress(Convert.ToInt32((double)DownloadSize / (double)FileSize * (double)100), String.Format("Загружено {0} %", Convert.ToInt32((double)DownloadSize / (double)FileSize * (double)100)));
                        outputStream.Write(buffer, 0, bytesRead);
                        bytesRead = stream.Read(buffer, 0, bufferSize);
                        outputStream.Flush();
                    }
                    outputStream.Close();
                    worker.ReportProgress(100);
                }
            }
            ZipFile.ExtractToDirectory($@"{downloadProduct.path}.zip", downloadProduct.path);
            File.Delete($@"{downloadProduct.path}.zip");

            download.Close();
        }
예제 #2
0
        private void Download_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog openFileDialog = new FolderBrowserDialog();

            Service.Product idg = profile.Games[mylibrary.SelectedIndex];
            //   openFileDialog.Filter = "All files (*.*)";
            // openFileDialog.CheckFileExists = false;
            openFileDialog.ShowDialog();
            string s = openFileDialog.SelectedPath;

            if (s != "")
            {
                s += @"\" + idg.Name;
                if (!File.Exists(s + "\\" + idg.Name + ".exe"))
                {
                    BackgroundWorker worker = new BackgroundWorker();
                    worker.RunWorkerCompleted   += worker_RunWorkerCompleted;
                    worker.WorkerReportsProgress = true;
                    worker.ProgressChanged      += worker_ProgressChanged;
                    worker.DoWork += worker_DoWork;
                    DownloadProduct downloadProduct = new DownloadProduct();
                    downloadProduct.path   = s;
                    downloadProduct.idUser = profile.ID;
                    downloadProduct.idGame = idg.Id;
                    SetPathToGame(s, idg.Id);
                    worker.RunWorkerAsync(downloadProduct);
                    MainWindow.shopWindows.Play.Visibility     = Visibility.Visible;
                    MainWindow.shopWindows.Download.Visibility = Visibility.Hidden;
                    //}
                    // dp.Download();
                }
                else
                {
                    UpdatePathToGame(s, idg.Id);
                    System.Windows.MessageBox.Show("По данному пути мы уже нашли эту игру, скачивать ничего не нужно");
                    Download.Visibility = Visibility.Hidden;
                    Play.Visibility     = Visibility.Visible;
                    //переназначить путь
                }
            }
        }