UpdateForm.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Windows.Forms;
  6. namespace ThCardReader
  7. {
  8. class UpdateForm : Form
  9. {
  10. private ProgressBar updateProgressBar;
  11. private Label progressLabel;
  12. private Button installButton;
  13. private readonly string version;
  14. private string filePath;
  15. public UpdateForm(string version)
  16. {
  17. this.version = version;
  18. InitializeComponent();
  19. }
  20. private void InitializeComponent()
  21. {
  22. this.updateProgressBar = new ProgressBar();
  23. this.progressLabel = new Label();
  24. this.installButton = new Button();
  25. this.SuspendLayout();
  26. //
  27. // updateProgressBar
  28. //
  29. this.updateProgressBar.Location = new System.Drawing.Point(22, 67);
  30. this.updateProgressBar.Name = "updateProgressBar";
  31. this.updateProgressBar.Size = new System.Drawing.Size(477, 34);
  32. this.updateProgressBar.TabIndex = 0;
  33. //
  34. // progressLabel
  35. //
  36. this.progressLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
  37. this.progressLabel.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  38. this.progressLabel.Location = new System.Drawing.Point(505, 67);
  39. this.progressLabel.Name = "progressLabel";
  40. this.progressLabel.Size = new System.Drawing.Size(59, 34);
  41. this.progressLabel.TabIndex = 1;
  42. this.progressLabel.Text = "0%";
  43. this.progressLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  44. //
  45. // installButton
  46. //
  47. this.installButton.Font = new System.Drawing.Font("宋体", 12F);
  48. this.installButton.Location = new System.Drawing.Point(191, 141);
  49. this.installButton.Name = "installButton";
  50. this.installButton.Size = new System.Drawing.Size(210, 37);
  51. this.installButton.TabIndex = 2;
  52. this.installButton.Text = "下载中...";
  53. this.installButton.UseVisualStyleBackColor = true;
  54. this.installButton.Click += new System.EventHandler(this.InstallButtonClick);
  55. this.installButton.Enabled = false;
  56. //
  57. // UpdateForm
  58. //
  59. this.ClientSize = new System.Drawing.Size(576, 261);
  60. this.Controls.Add(this.installButton);
  61. this.Controls.Add(this.progressLabel);
  62. this.Controls.Add(this.updateProgressBar);
  63. this.Name = "UpdateForm";
  64. this.StartPosition = FormStartPosition.CenterScreen;
  65. this.Text = "读卡插件更新";
  66. this.Load += new EventHandler(this.UpdateForm_Load);
  67. this.ResumeLayout(false);
  68. }
  69. private void UpdateForm_Load(object sender, EventArgs e)
  70. {
  71. ExecuteUpdate();
  72. }
  73. private void ExecuteUpdate()
  74. {
  75. filePath = String.Format("D:\\读卡插件-{0}.msi", version);
  76. using (WebClient webClient = new WebClient())
  77. {
  78. try
  79. {
  80. webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(ThDownloadCompleted);
  81. webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ThDownloadProgressChanged);
  82. webClient.DownloadFileAsync(new Uri("http://webhis.thyy.cn:8080/download/readcard/读卡插件.msi"), filePath);
  83. }
  84. catch (Exception ex)
  85. {
  86. MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  87. }
  88. }
  89. }
  90. private void ThDownloadCompleted(object sender, AsyncCompletedEventArgs e)
  91. {
  92. this.installButton.Text = "下载完成,点击安装";
  93. this.installButton.Enabled = true;
  94. }
  95. private void ThDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  96. {
  97. this.updateProgressBar.Minimum = 0;
  98. this.updateProgressBar.Maximum = (int)e.TotalBytesToReceive;
  99. this.updateProgressBar.Value = (int)e.BytesReceived;
  100. this.progressLabel.Text = e.ProgressPercentage + "%";
  101. }
  102. private void InstallButtonClick(object sender, EventArgs e)
  103. {
  104. Process.Start(filePath);
  105. Process.GetCurrentProcess().Kill();
  106. }
  107. }
  108. }