Ejemplo n.º 1
0
        /// <summary>
        /// Downloads the driver and some other stuff
        /// </summary>
        private static void downloadDriver()
        {
            int DateDiff = (DateTime.Now - releaseDate).Days; // how many days between the two dates
            string theDate = null;

            if (DateDiff == 1) {
                theDate = DateDiff + " day ago";
            } else if (DateDiff < 1) {
                theDate = "today";
            } else {
                theDate = DateDiff + " days ago";
            }

            var message = "There is new gpu drivers up for download, do you want to download them now?" + Environment.NewLine + Environment.NewLine;

            string key = "Show Driver Description";
            string val = null;

            // loop
            while (val != "true" & val != "false") {
                val = SettingManager.readSetting(key); // refresh value each time

                if (val == "true") {
                    message = message + "Description: " + releaseDesc + Environment.NewLine;
                } else if (val == "false") {
                    break;
                } else {
                    // invalid value
                    SettingManager.setupSetting(key);
                }
            }

            message = message + "Driver version: " + OnlineGPUVersion + " (you're running " + OfflineGPUVersion + ")" + Environment.NewLine +
                        "Driver released: " + theDate + " (" + releaseDate.ToShortDateString() + ")";

            DialogResult dialog = MessageBox.Show(message, "TinyNvidiaUpdateChecker", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dialog == DialogResult.Yes) {

                Console.WriteLine();

                // @todo error handling could be better:
                // isolate saveFileDialog errors with accually downloading GPU driver

                // @todo add status bar for download progress
                // @todo do the saveFileDialog in a loop

                bool error = false;
                try
                {
                    string driverName = downloadURL.Split('/').Last(); // retrives file name from url

                    DialogResult result;

                    using (SaveFileDialog saveFileDialog = new SaveFileDialog()) {
                        saveFileDialog.Filter = "Executable|*.exe";
                        saveFileDialog.Title = "Choose save file for GPU driver";
                        saveFileDialog.FileName = driverName;

                        result = saveFileDialog.ShowDialog(); // show dialog and get status (will wait for input)

                        switch (result)
                        {
                            case DialogResult.OK:
                                savePath = saveFileDialog.FileName.ToString();
                                break;

                            default:
                                // savePath = Path.GetTempPath() + driverName;

                                // if something went wrong, fall back to downloads folder
                                savePath = getDownloadFolderPath() + driverName;
                                break;
                        }
                    }

                    if (debug == true) {
                        Console.WriteLine("savePath: " + savePath);
                        Console.WriteLine("result: " + result);
                    }

                    Console.Write("Downloading the driver . . . ");

                    using (WebClient webClient = new WebClient())
                    {
                        var notifier = new AutoResetEvent(false);
                        var progress = new ProgressBar();

                        webClient.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e)
                        {
                            progress.Report((double)e.ProgressPercentage / 100);

                            if (e.BytesReceived >= e.TotalBytesToReceive) notifier.Set();
                        };

                        webClient.DownloadFileAsync(new Uri(downloadURL), savePath);

                        notifier.WaitOne(); // sync with the above
                        progress.Dispose(); // get rid of the progress bar
                    }

                }
                catch (Exception ex)
                {
                    error = true;
                    Console.Write("ERROR!");
                    LogManager.log(ex.Message, LogManager.Level.ERROR);
                    Console.WriteLine();
                    Console.WriteLine(ex.StackTrace);
                    Console.WriteLine();
                }

                if (error == false)
                {
                    Console.Write("OK!");
                    Console.WriteLine();
                }

                Console.WriteLine();
                Console.WriteLine("The downloaded file has been saved at: " + savePath);

                dialog = MessageBox.Show("Do you want view the release PDF?", "TinyNvidiaUpdateChecker", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes) {
                    try {
                        Process.Start(pdfURL);
                    }
                    catch (Exception ex) {
                        Console.WriteLine(ex.StackTrace);
                    }
                }

                dialog = MessageBox.Show("Do you wish to run the driver installer?", "TinyNvidiaUpdateChecker", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes) {
                    try {
                        Process.Start(savePath);
                    } catch (Exception ex) {
                        Console.WriteLine(ex.StackTrace);
                    }

                }
            }
        }