using System; using System.ComponentModel; using System.Diagnostics; using System.Net; using System.Windows.Forms; namespace ThCardReader { class UpdateForm : Form { private ProgressBar updateProgressBar; private Label progressLabel; private Button installButton; private readonly string version; private string filePath; public UpdateForm(string version) { this.version = version; InitializeComponent(); } private void InitializeComponent() { this.updateProgressBar = new ProgressBar(); this.progressLabel = new Label(); this.installButton = new Button(); this.SuspendLayout(); // // updateProgressBar // this.updateProgressBar.Location = new System.Drawing.Point(22, 67); this.updateProgressBar.Name = "updateProgressBar"; this.updateProgressBar.Size = new System.Drawing.Size(477, 34); this.updateProgressBar.TabIndex = 0; // // progressLabel // this.progressLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.progressLabel.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.progressLabel.Location = new System.Drawing.Point(505, 67); this.progressLabel.Name = "progressLabel"; this.progressLabel.Size = new System.Drawing.Size(59, 34); this.progressLabel.TabIndex = 1; this.progressLabel.Text = "0%"; this.progressLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // installButton // this.installButton.Font = new System.Drawing.Font("宋体", 12F); this.installButton.Location = new System.Drawing.Point(191, 141); this.installButton.Name = "installButton"; this.installButton.Size = new System.Drawing.Size(210, 37); this.installButton.TabIndex = 2; this.installButton.Text = "下载中..."; this.installButton.UseVisualStyleBackColor = true; this.installButton.Click += new System.EventHandler(this.InstallButtonClick); this.installButton.Enabled = false; // // UpdateForm // this.ClientSize = new System.Drawing.Size(576, 261); this.Controls.Add(this.installButton); this.Controls.Add(this.progressLabel); this.Controls.Add(this.updateProgressBar); this.Name = "UpdateForm"; this.StartPosition = FormStartPosition.CenterScreen; this.Text = "读卡插件更新"; this.Load += new EventHandler(this.UpdateForm_Load); this.ResumeLayout(false); } private void UpdateForm_Load(object sender, EventArgs e) { ExecuteUpdate(); } private void ExecuteUpdate() { filePath = String.Format("D:\\读卡插件-{0}.msi", version); using (WebClient webClient = new WebClient()) { try { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(ThDownloadCompleted); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ThDownloadProgressChanged); webClient.DownloadFileAsync(new Uri("http://webhis.thyy.cn:8080/download/readcard/读卡插件.msi"), filePath); } catch (Exception ex) { MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ThDownloadCompleted(object sender, AsyncCompletedEventArgs e) { this.installButton.Text = "下载完成,点击安装"; this.installButton.Enabled = true; } private void ThDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { this.updateProgressBar.Minimum = 0; this.updateProgressBar.Maximum = (int)e.TotalBytesToReceive; this.updateProgressBar.Value = (int)e.BytesReceived; this.progressLabel.Text = e.ProgressPercentage + "%"; } private void InstallButtonClick(object sender, EventArgs e) { Process.Start(filePath); Process.GetCurrentProcess().Kill(); } } }