Downloader.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. namespace VeloeMinecraftLauncher.MinecraftLauncher
  11. {
  12. internal static class Downloader
  13. {
  14. public static T DownloadAndDeserializeJsonData<T>(string url) where T : new()
  15. {
  16. using (var webClient = new HttpClient())
  17. {
  18. var jsonData = string.Empty;
  19. try
  20. {
  21. jsonData = webClient.GetStringAsync(url).Result;
  22. }
  23. catch (Exception) { }
  24. return string.IsNullOrEmpty(jsonData)
  25. ? new T()
  26. : JsonSerializer.Deserialize<T>(jsonData);
  27. }
  28. }
  29. public static void DownloadFile(string url, string path, string filename)
  30. {
  31. using (var webClient = new WebClient())
  32. {
  33. if (!Directory.Exists(path))
  34. Directory.CreateDirectory(path);
  35. webClient.DownloadFileAsync(new System.Uri(url), path + "\\" + filename);
  36. }
  37. }
  38. }
  39. }