⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formupdate.cs

📁 即时通讯系统LanMsg
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
namespace LanMsg
{
	/// <summary>
	/// FormUpdate 的摘要说明。
	/// </summary>
	public class FormUpdate : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private LanMsg.XpProgressBar.XpProgressBar ProgressBar1;
		private System.Windows.Forms.Timer timerFileInfo;
		private System.Windows.Forms.Button butOk;
		private System.ComponentModel.IContainer components;

		public FormUpdate()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.butOk = new System.Windows.Forms.Button();
			this.ProgressBar1 = new LanMsg.XpProgressBar.XpProgressBar();
			this.label1 = new System.Windows.Forms.Label();
			this.timerFileInfo = new System.Windows.Forms.Timer(this.components);
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.butOk);
			this.groupBox1.Controls.Add(this.ProgressBar1);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(18, 11);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(256, 120);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "升级信息";
			// 
			// butOk
			// 
			this.butOk.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.butOk.Location = new System.Drawing.Point(92, 88);
			this.butOk.Name = "butOk";
			this.butOk.Size = new System.Drawing.Size(72, 24);
			this.butOk.TabIndex = 2;
			this.butOk.Text = "确定";
			this.butOk.Click += new System.EventHandler(this.butOk_Click);
			// 
			// ProgressBar1
			// 
			this.ProgressBar1.ColorBackGround = System.Drawing.Color.White;
			this.ProgressBar1.ColorBarBorder = System.Drawing.Color.FromArgb(((System.Byte)(170)), ((System.Byte)(240)), ((System.Byte)(170)));
			this.ProgressBar1.ColorBarCenter = System.Drawing.Color.FromArgb(((System.Byte)(10)), ((System.Byte)(150)), ((System.Byte)(10)));
			this.ProgressBar1.ColorText = System.Drawing.Color.Black;
			this.ProgressBar1.Location = new System.Drawing.Point(29, 59);
			this.ProgressBar1.Name = "ProgressBar1";
			this.ProgressBar1.Position = 0;
			this.ProgressBar1.PositionMax = 100;
			this.ProgressBar1.PositionMin = 0;
			this.ProgressBar1.Size = new System.Drawing.Size(198, 16);
			this.ProgressBar1.TabIndex = 1;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 17);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(240, 31);
			this.label1.TabIndex = 0;
			this.label1.Text = "    LanMsg已检测到最新的版本,请单击确定按钮,下载更新程序。";
			// 
			// timerFileInfo
			// 
			this.timerFileInfo.Interval = 200;
			this.timerFileInfo.Tick += new System.EventHandler(this.timerFileInfo_Tick);
			// 
			// FormUpdate
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 143);
			this.ControlBox = false;
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FormUpdate";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "LanMsg 智能更新程序";
			this.TopMost = true;
			this.Load += new System.EventHandler(this.FormUpdate_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private string address= "http://www.163.com/s/LanMsgUpdate.exe";//要下载的升级包网址
		private string downloadFile=Application.StartupPath +"\\LanMsgUpdate.exe" ;//设置下载软件保存的路径
        private long DownLoadFileSize=0;


		private void FormUpdate_Load(object sender, System.EventArgs e)
		{
			try
			{
				if(System.IO.File.Exists(downloadFile))
					System.IO.File.Delete(downloadFile);//如果旧的更新包
				DownLoadFileSize=GetDownLoadFileSize();//获得下载文件大小 
				this.ProgressBar1.PositionMax =(int)DownLoadFileSize;
			}
			catch{}
		}

		private void DownLoad()//执行下载任务
		{
			WebClient dfile = new WebClient(); //实例化WebClient类
			dfile.DownloadFile(this.address,this.downloadFile);//下载指定nrl地址的文件 
            this.Hide();
			MessageBox.Show("LanMsg升级包已经下载完成,单击确定执行更新。","升级提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information );
			System.Diagnostics.Process.Start(downloadFile);//运行刚更新的应用程序
			this.Close();
		}

		private void DownLoadProcess() //获得正在下载的文件的已下载大小
		{
			if(!System.IO.File.Exists(downloadFile))return;
			System.IO.FileInfo fileinfo=new System.IO.FileInfo(downloadFile);
			this.ProgressBar1.Position=(int)fileinfo.Length;
		}

		private long GetDownLoadFileSize()//获得下载文件大小 
		{
			WebRequest th= WebRequest.Create(address);//
			WebResponse w  = th.GetResponse();
			return w.ContentLength;
		}

		private void timerFileInfo_Tick(object sender, System.EventArgs e)
		{
			DownLoadProcess();
		}

		private void butOk_Click(object sender, System.EventArgs e)
		{
			this.butOk.Enabled=false;
			this.label1.Text="正在下载LanMsg更新程序包...";
			this.timerFileInfo.Enabled=true;

			System.Threading.Thread t = new  System.Threading.Thread(new System.Threading.ThreadStart(DownLoad));
			t.Start();//执行下载任务
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -