コード例 #1
0
 private static void DownloadHelper(
     DownloadableFile[] downloadableFiles,
     int retry = 1,
     System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null,
     System.ComponentModel.AsyncCompletedEventHandler onDownloadFileCompleted = null)
 {
     if (downloadableFiles == null || downloadableFiles.Length == 0)
     {
         if (onDownloadFileCompleted != null)
         {
             onDownloadFileCompleted(null, null);
         }
     }
     else if (downloadableFiles.Length == 1)
     {
         DownloadHelper(downloadableFiles[0], retry, onDownloadProgressChanged, onDownloadFileCompleted);
     }
     else
     {
         DownloadableFile   currentFile    = downloadableFiles[0];
         DownloadableFile[] remainingFiles = new DownloadableFile[downloadableFiles.Length - 1];
         Array.Copy(downloadableFiles, 1, remainingFiles, 0, remainingFiles.Length);
         DownloadHelper(currentFile, retry, onDownloadProgressChanged,
                        (object sender, System.ComponentModel.AsyncCompletedEventArgs e) =>
         {
             DownloadHelper(remainingFiles, retry, onDownloadProgressChanged, onDownloadFileCompleted);
         }
                        );
     }
 }
コード例 #2
0
 /// <summary>
 /// This event is raised each time file download operation completes.
 /// </summary>
 private void Gett_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
 {
     System.ComponentModel.AsyncCompletedEventHandler copyDownloadAsyncFileCompleted = DownloadAsyncFileCompleted;
     if (copyDownloadAsyncFileCompleted != null)
     {
         copyDownloadAsyncFileCompleted(this, e);
     }
 }
コード例 #3
0
 private static void Download(
     DownloadableFile[] files,
     int retry = 1,
     System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null,
     System.ComponentModel.AsyncCompletedEventHandler onDownloadFileCompleted = null)
 {
     DownloadHelper(files, retry, onDownloadProgressChanged, onDownloadFileCompleted);
 }
コード例 #4
0
        /// <summary>
        /// 异步获取网络图片,使用时务必重写BitmapImage参数的完成事件
        /// </summary>
        /// <param name="uri">网络uri</param>
        /// <param name="FunImageDownloadCompleted">完成时运行的函数</param>
        /// <returns></returns>
        public string GetAlbumImageSource(string uri, System.ComponentModel.AsyncCompletedEventHandler FunImageDownloadCompleted)
        {
            Uri    uri2     = new Uri(uri, UriKind.Absolute);
            string filename = uri2.Segments[uri2.Segments.Count() - 1];

            web = new WebClient();
            web.DownloadFileCompleted += FunImageDownloadCompleted;
            web.DownloadFileAsync(uri2, filename);
            return(filename);
        }
コード例 #5
0
 /// <summary>
 /// 异步下载文件(带下载完成后的处理)
 /// </summary>
 /// <param name="url">文件URL地址</param>
 /// <param name="filename">文件保存完整路径</param>
 /// <param name="handler">下载完成后调用的方法</param>
 public static void DownloadFileAsync(string url, string filename, System.ComponentModel.AsyncCompletedEventHandler handler)
 {
     try
     {
         System.Net.WebClient wc = new System.Net.WebClient();
         wc.DownloadFileAsync(new Uri(url), filename);
         if (handler != null)
         {
             wc.DownloadFileCompleted += handler;
         }
     }
     catch
     {
     }
 }
コード例 #6
0
        private void DownloadFile(AppToInstall app,
                                  System.ComponentModel.AsyncCompletedEventHandler completedDownload, DownloadProgressChangedEventHandler progress, ProgressBar progressBar)
        {
            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()
            {
                SelectedPath = RootPath, ShowNewFolderButton = true
            })
            {
                DialogResult result = folderBrowserDialog.ShowDialog();
                if (result != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }

                progressBar.Visibility = Visibility.Visible;

                string path = folderBrowserDialog.SelectedPath;

                _manage.DownloadFile(path, app, completedDownload, progress);
            }
        }
コード例 #7
0
        public static void DownloadUpdateA(string url, System.Net.DownloadProgressChangedEventHandler onprogr, System.ComponentModel.AsyncCompletedEventHandler oncomplete)
        {
            try
            {
                if (client == null)
                {
                    if (System.IO.File.Exists(installer))
                    {
                        System.IO.File.Delete(installer);
                    }

                    client = new System.Net.WebClient();
                    client.DownloadProgressChanged += onprogr;
                    client.DownloadFileCompleted   += disposeclient;
                    client.DownloadFileCompleted   += oncomplete;

                    client.DownloadFileAsync(new System.Uri(url), installer);
                }
                else
                {
                    oncomplete(null, new System.ComponentModel.AsyncCompletedEventArgs(new InvalidOperationException("Download already in progress!"), true, null));
                }
            }
            catch (Exception ex)
            {
                oncomplete(null, new System.ComponentModel.AsyncCompletedEventArgs(new InvalidOperationException("Error downloading!"), true, null));
            }
        }
コード例 #8
0
        /// <summary>
        /// Download and unzip the latest QCAlgorithm to a local folder:
        /// </summary>
        /// <param name="destination"></param>
        public void DownloadQCAlgorithm(string directory, bool async = false, DownloadProgressChangedEventHandler callbackProgress = null, System.ComponentModel.AsyncCompletedEventHandler callbackCompleted = null)
        {
            try
            {
                if (!System.IO.Directory.Exists(directory))
                {
                    System.IO.Directory.CreateDirectory(directory);
                }

                string file = directory + @"\" + "QCAlgorithm.zip";

                using (var client = new WebClient())
                {
                    if (async)
                    {
                        client.DownloadFileAsync(new Uri(@"https://github.com/QuantConnect/QCAlgorithm/archive/master.zip"), file);
                        if (callbackProgress != null)
                        {
                            client.DownloadProgressChanged += callbackProgress;
                        }
                        if (callbackCompleted != null)
                        {
                            client.DownloadFileCompleted += callbackCompleted;
                        }
                    }
                    else
                    {
                        client.DownloadFile(@"https://github.com/QuantConnect/QCAlgorithm/archive/master.zip", file);
                    }
                }

                //Extract the zip:
                WriteZip(directory, file);

                //Save the new Hash ID to the disk:
                System.IO.File.WriteAllText(_hashFile, GetLatestQCAlgorithmSHA());
            }
            catch (Exception err)
            {
                Console.WriteLine("QuantConnect.RestAPI.DownloadQCAlgorithm(): " + err.Message);
            }
        }
コード例 #9
0
        public static async Task UpdateToNewVersion(DownloadProgressChangedEventHandler dpceh, System.ComponentModel.AsyncCompletedEventHandler aceh)
        {
            string rawResponse = await WebHelper.Get(UPDATE_ENDPOINT);

            LatestReleaseResponse response = Json.Parse <LatestReleaseResponse>(rawResponse);

            if (response.assets.Length == 0)
            {
                MessageBox.Show("Unable to download new executable. Please update manually from the Github releases page.");
                return;
            }

            //First download link is fine for now... probably
            var link = response.assets[0].browser_download_url;

            var       downloadPath = Path.GetTempFileName();
            WebClient wc           = new WebClient();

            wc.DownloadProgressChanged += dpceh;
            wc.DownloadFileCompleted   += aceh;
            await wc.DownloadFileTaskAsync(link, downloadPath);

            //Rename current executable as backup
            try
            {
                //Wait for a second before restarting so we get to see our nice green finished bar
                await Task.Delay(1000);

                string exeName = Process.GetCurrentProcess().ProcessName;
                File.Delete(exeName + ".bak");
                File.Move(exeName + ".exe", exeName + ".bak");
                File.Move(downloadPath, exeName + ".exe");

                Process.Start(exeName + ".exe");
                Environment.Exit(0);
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to replace with updated executable. Check whether the executable is marked as read-only, or whether it is in a protected folder that requires administrative rights.");
            }
        }
コード例 #10
0
        private static void DownloadHelper(
            DownloadableFile downloadableFile,
            int retry = 1,
            System.Net.DownloadProgressChangedEventHandler onDownloadProgressChanged = null,
            System.ComponentModel.AsyncCompletedEventHandler onDownloadFileCompleted = null
            )
        {
            if (downloadableFile.Url == null)
            {
                return;
            }

            //uncomment the following line to force re-download every time.
            //File.Delete(downloadableFile.LocalFile);
            if (!File.Exists(downloadableFile.LocalFile) || new FileInfo(downloadableFile.LocalFile).Length == 0)
            {
                try
                {
                    //Download the file
                    Trace.WriteLine("downloading file from:" + downloadableFile.Url + " to: " + downloadableFile.LocalFile);
                    System.Net.WebClient downloadClient = new System.Net.WebClient();

                    if (onDownloadProgressChanged != null)
                    {
                        downloadClient.DownloadProgressChanged += onDownloadProgressChanged;
                    }
                    if (onDownloadFileCompleted != null)
                    {
                        downloadClient.DownloadFileCompleted += onDownloadFileCompleted;
                    }

                    downloadClient.DownloadFileAsync(new Uri(downloadableFile.Url), downloadableFile.LocalFile);
                }
                catch (Exception e)
                {
                    if (File.Exists(downloadableFile.LocalFile))
                    {
                        //The downloaded file may be corrupted, should delete it
                        File.Delete(downloadableFile.LocalFile);
                    }

                    if (retry > 0)
                    {
                        DownloadHelper(downloadableFile, retry - 1);
                    }
                    else
                    {
#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE
                        UnityEngine.Debug.Log(e.StackTrace);
#else
                        Debug.WriteLine(e);
#endif
                        throw;
                    }
                }
            }
            else
            {
                if (onDownloadFileCompleted != null)
                {
                    onDownloadFileCompleted(null, null);
                }
            }
        }
コード例 #11
0
        public static async Task UpdateToNewVersion(DownloadProgressChangedEventHandler dpceh, System.ComponentModel.AsyncCompletedEventHandler aceh)
        {
            var       downloadPath = Path.GetTempFileName();
            WebClient wc           = new WebClient();

            wc.DownloadProgressChanged += dpceh;
            wc.DownloadFileCompleted   += aceh;
            await wc.DownloadFileTaskAsync(exeUrl, downloadPath);

            //Rename current executable as backup
            try
            {
                //Wait for a second before restarting
                await Task.Delay(1000);

                string exeName = Process.GetCurrentProcess().ProcessName;
                File.Delete(exeName + ".bak");
                File.Move(exeName + ".exe", exeName + ".bak");
                File.Move(downloadPath, exeName + ".exe");

                Process.Start(exeName + ".exe");
                Environment.Exit(0);
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to replace with updated executable. Check whether the executable is marked as read-only, or whether it is in a protected folder that requires administrative rights.");
            }
        }