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 5.6 kB

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