You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using COSXML.Auth;
  2. using COSXML.CosException;
  3. using COSXML.Model.Object;
  4. using COSXML;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Net;
  8. using System.Text;
  9. using Newtonsoft.Json;
  10. using System.Windows.Markup;
  11. using System;
  12. using System.Security.Cryptography;
  13. using System.Diagnostics;
  14. using System.Windows;
  15. using System.Windows.Documents;
  16. namespace Program
  17. {
  18. class Updater
  19. {
  20. public static string Dir = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName)
  21. ?? throw new Exception("Cannot get current directory");
  22. public static string InstallerName = "Installer.exe";
  23. public static string jsonKey = "installerHash.json";
  24. public static string KeyHead = "Installer/";
  25. public static bool UpdateInstaller()
  26. {
  27. string json;
  28. try
  29. {
  30. using (StreamReader r = new StreamReader(System.IO.Path.Combine(Dir, "updateList.json")))
  31. json = r.ReadToEnd();
  32. json = json.Replace("\r", string.Empty).Replace("\n", string.Empty);
  33. List<string> jsonList = JsonConvert.DeserializeObject<List<string>>(json)
  34. ?? throw new Exception("Failed to deserialize json!");
  35. foreach (string todo in jsonList)
  36. {
  37. if (!todo.Equals("None"))
  38. {
  39. File.Delete(Path.Combine(Dir, todo));
  40. download(Path.Combine(Dir, todo), KeyHead + todo);
  41. }
  42. }
  43. }
  44. catch (IOException ex)
  45. {
  46. MessageBox.Show($"下载器本体未能成功关闭:\n{ex}");
  47. return false;
  48. }
  49. catch (Exception ex)
  50. {
  51. MessageBox.Show($"尝试下载时出现问题:\n{ex}\n{ex.StackTrace}");
  52. return false;
  53. }
  54. return true;
  55. }
  56. public static int TellDismiss()
  57. {
  58. try
  59. {
  60. string savepath = System.IO.Path.Combine(Dir, "updateList.json");
  61. FileStream fs = new FileStream(savepath, FileMode.Open, FileAccess.ReadWrite);
  62. StreamReader sr = new StreamReader(fs);
  63. string json = sr.ReadToEnd();
  64. if (json == null || json == "")
  65. {
  66. json += @"{""None""}";
  67. }
  68. List<string> ls = new List<string>();
  69. ls = JsonConvert.DeserializeObject<List<string>>(json)
  70. ?? throw new Exception("Failed to deserialize json!");
  71. if (!ls.Contains("Dismiss"))
  72. {
  73. ls.Add("Dismiss");
  74. }
  75. sr.Close();
  76. fs.Close();
  77. StreamWriter sw = new StreamWriter(System.IO.Path.Combine(Dir, "updateList.json"), false);
  78. sw.WriteLine(JsonConvert.SerializeObject(ls));
  79. sw.Close();
  80. return 0;//成功
  81. }
  82. catch
  83. {
  84. return -1;//失败
  85. }
  86. }
  87. public static void download(string download_dir, string key)
  88. {
  89. // download_dir标记根文件夹路径,key为相对根文件夹的路径(不带./)
  90. // 初始化CosXmlConfig(提供配置SDK接口)
  91. string appid = "1314234950"; // 设置腾讯云账户的账户标识(APPID)
  92. string region = "ap-beijing"; // 设置一个默认的存储桶地域
  93. CosXmlConfig config = new CosXmlConfig.Builder()
  94. .IsHttps(true) // 设置默认 HTTPS 请求
  95. .SetAppid(appid) // 设置腾讯云账户的账户标识 APPID
  96. .SetRegion(region) // 设置一个默认的存储桶地域
  97. .SetDebugLog(true) // 显示日志
  98. .Build(); // 创建 CosXmlConfig 对象
  99. // 永久密钥访问凭证
  100. string secretId = "***"; //"云 API 密钥 SecretId";
  101. string secretKey = "***"; //"云 API 密钥 SecretKey";
  102. long durationSecond = 1000; // 每次请求签名有效时长,单位为秒
  103. QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
  104. secretId, secretKey, durationSecond
  105. );
  106. // 初始化 CosXmlServer
  107. CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider);
  108. // 创建存储桶
  109. try
  110. {
  111. string bucket = "thuai6-1314234950"; // 格式:BucketName-APPID
  112. string localDir = System.IO.Path.GetDirectoryName(download_dir) // 本地文件夹
  113. ?? throw new Exception("Failed to get directory name!");
  114. string localFileName = System.IO.Path.GetFileName(download_dir); // 指定本地保存的文件名
  115. GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);
  116. Dictionary<string, string> test = request.GetRequestHeaders();
  117. request.SetCosProgressCallback(delegate (long completed, long total)
  118. {
  119. //Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  120. });
  121. // 执行请求
  122. GetObjectResult result = cosXml.GetObject(request);
  123. // 请求成功
  124. }
  125. catch (CosClientException clientEx)
  126. {
  127. throw clientEx;
  128. }
  129. catch (CosServerException serverEx)
  130. {
  131. throw serverEx;
  132. }
  133. }
  134. }
  135. }