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

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