2024-09-22 09:03:36 -04:00

61 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MxWDB.Utilities
{
class Web
{
public static void DownloadFileAsync(string url, string file)
{
using (WebClient wc = new WebClient())
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
wc.DownloadProgressChanged += DownloadProgressChanged;
var syncObject = new object();
lock (syncObject)
{
Uri uri = new Uri(url);
wc.DownloadFileAsync(uri, file);
Monitor.Wait(syncObject);
}
}
}
private static void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.SetCursorPosition(0, Program.line+4);
Console.Write($"DL: ");
Console.SetCursorPosition(3, Program.line + 4);
Console.Write($"{e.BytesReceived/1024}");
}
public static void DownloadFileNoAsync(string url, string file)
{
using (WebClient wc = new WebClient())
{
try
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
Uri uri = new Uri(url);
wc.DownloadFile(uri, file);
}
catch (WebException ex)
{
MSG.CMW($"[web] {ex.Message}", true, 3);
MSG.CMW($"[web] URL: {url}", true, 3);
}
catch (Exception ex)
{
MSG.CMW($"[web] {ex.Message}", true, 3);
}
}
}
}
}