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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. namespace Program
  15. {
  16. class Updater
  17. {
  18. public static string Dir = Directory.GetCurrentDirectory();
  19. public static string InstallerName = "Installer.exe";
  20. public static string jsonKey = "installerHash.json";
  21. public static string KeyHead = "Installer/";
  22. public static bool UpdateInstaller()
  23. {
  24. string json;
  25. try
  26. {
  27. using (StreamReader r = new StreamReader(System.IO.Path.Combine(Dir, "updateList.json")))
  28. json = r.ReadToEnd();
  29. json = json.Replace("\r", string.Empty).Replace("\n", string.Empty);
  30. List<string> jsonList = JsonConvert.DeserializeObject<List<string>>(json);
  31. foreach (string todo in jsonList)
  32. {
  33. File.Delete(Path.Combine(Dir, todo));
  34. download(Path.Combine(Dir, todo), KeyHead + todo);
  35. }
  36. }
  37. catch
  38. {
  39. return false;
  40. }
  41. return true;
  42. }
  43. public static void download(string download_dir, string key)
  44. {
  45. // download_dir标记根文件夹路径,key为相对根文件夹的路径(不带./)
  46. // 初始化CosXmlConfig(提供配置SDK接口)
  47. string appid = "1314234950"; // 设置腾讯云账户的账户标识(APPID)
  48. string region = "ap-beijing"; // 设置一个默认的存储桶地域
  49. CosXmlConfig config = new CosXmlConfig.Builder()
  50. .IsHttps(true) // 设置默认 HTTPS 请求
  51. .SetAppid(appid) // 设置腾讯云账户的账户标识 APPID
  52. .SetRegion(region) // 设置一个默认的存储桶地域
  53. .SetDebugLog(true) // 显示日志
  54. .Build(); // 创建 CosXmlConfig 对象
  55. // 永久密钥访问凭证
  56. string secretId = "***"; //"云 API 密钥 SecretId";
  57. string secretKey = "***"; //"云 API 密钥 SecretKey";
  58. long durationSecond = 1000; // 每次请求签名有效时长,单位为秒
  59. QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
  60. secretId, secretKey, durationSecond
  61. );
  62. // 初始化 CosXmlServer
  63. CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider);
  64. // 创建存储桶
  65. try
  66. {
  67. string bucket = "thuai6-1314234950"; // 格式:BucketName-APPID
  68. string localDir = System.IO.Path.GetDirectoryName(download_dir); // 本地文件夹
  69. string localFileName = System.IO.Path.GetFileName(download_dir); // 指定本地保存的文件名
  70. GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);
  71. Dictionary<string, string> test = request.GetRequestHeaders();
  72. request.SetCosProgressCallback(delegate (long completed, long total)
  73. {
  74. //Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  75. });
  76. // 执行请求
  77. GetObjectResult result = cosXml.GetObject(request);
  78. // 请求成功
  79. }
  80. catch (CosClientException clientEx)
  81. {
  82. throw clientEx;
  83. }
  84. catch (CosServerException serverEx)
  85. {
  86. throw serverEx;
  87. }
  88. }
  89. }
  90. }