Example #1
0
 public void DownloadRemoteFile(string url, string destination)
 {
     try
     {
         using (WebClient wc = new WebClientWithTimeout())
         {
             wc.Proxy = null;
             wc.DownloadFileCompleted += (s, e) =>
             {
                 Console.Write("100%");
                 Console.WriteLine();
                 Utils.Log($"Downloaded {url} to {destination}");
                 counter = 0;
             };
             wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
             Stream stream = wc.OpenRead(url);
             // Size manipulations
             string size;
             long   bytes = Convert.ToInt64(wc.ResponseHeaders["Content-Length"]);
             double mb    = Math.Round((bytes / 1024f) / 1024f, 2);
             if (mb <= 0.00f)
             {
                 size = Math.Round((bytes / 1024f), 2).ToString() + "KB";
             }
             else
             {
                 size = mb.ToString() + "MB";
             }
             stream.Close();
             Utils.Log($"Downloading {Path.GetFileName(destination)}: {size}...");
             wc.DownloadFileTaskAsync(new Uri(url), destination).Wait();
             wc.Dispose();
         }
     }
     catch (WebException we)
     {
         Utils.Log(we.Message, Utils.MsgType.error);
     }
 }