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

📄 form1.cs

📁 使用C#语言实现软件自动升级的实例源码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Threading;
using System.IO;
using Microsoft.Win32;

namespace Login
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.ComboBox combUsers;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnOK;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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.txtPassword = new System.Windows.Forms.TextBox();
			this.combUsers = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.btnOK = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(113, 64);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new System.Drawing.Size(120, 21);
			this.txtPassword.TabIndex = 30;
			this.txtPassword.Text = "";
			// 
			// combUsers
			// 
			this.combUsers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.combUsers.Location = new System.Drawing.Point(112, 32);
			this.combUsers.Name = "combUsers";
			this.combUsers.Size = new System.Drawing.Size(121, 20);
			this.combUsers.TabIndex = 31;
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.BackColor = System.Drawing.Color.Transparent;
			this.label2.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.label2.Location = new System.Drawing.Point(56, 66);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(42, 17);
			this.label2.TabIndex = 33;
			this.label2.Text = "密  码";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// btnCancel
			// 
			this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnCancel.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnCancel.ImageIndex = 3;
			this.btnCancel.Location = new System.Drawing.Point(168, 104);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(62, 32);
			this.btnCancel.TabIndex = 29;
			this.btnCancel.Text = "取消(&C)";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.BackColor = System.Drawing.Color.Transparent;
			this.label1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.label1.Location = new System.Drawing.Point(56, 34);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(42, 17);
			this.label1.TabIndex = 32;
			this.label1.Text = "用户名";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// btnOK
			// 
			this.btnOK.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnOK.Enabled = false;
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnOK.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnOK.ImageIndex = 2;
			this.btnOK.Location = new System.Drawing.Point(80, 104);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(62, 32);
			this.btnOK.TabIndex = 28;
			this.btnOK.Text = "确定(&O)";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 165);
			this.Controls.Add(this.txtPassword);
			this.Controls.Add(this.combUsers);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.btnOK);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		/// <summary>
		/// 存放操作员及密码的DataSet
		/// </summary>
		private DataSet m_DataSet;
		/// <summary>
		/// 本功能用到的数据库表
		/// </summary>
		private string m_TableName="操作员";
		private DataTable m_Table;

		/// <summary>
		/// 定义本软件在注册表中software下的公司名和软件名称
		/// </summary>
		private string m_companyname="lqjt",m_softwarename="autologin";
		/// <summary>
		/// 从注册表中读信息;
		/// </summary>
		/// <param name="p_KeyName">要读取的键值</param>
		/// <returns>读到的键值字符串,如果失败(如注册表尚无信息),则返回""</returns>
		private string ReadInfo(string p_KeyName)
		{
			RegistryKey SoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);
			RegistryKey CompanyKey=SoftwareKey.OpenSubKey(m_companyname);
			string strValue="";
			
			if(CompanyKey==null)
				return "";
			RegistryKey SoftwareNameKey=CompanyKey.OpenSubKey(m_softwarename);//建立
			if(SoftwareNameKey==null)
				return "";

			try
			{
				strValue=SoftwareNameKey.GetValue(p_KeyName).ToString().Trim();
			}
			catch
			{}

			if(strValue==null)
				strValue="";
			return strValue;
		}
		/// <summary>
		/// 将信息写入注册表
		/// </summary>
		/// <param name="p_keyname">键名</param>
		/// <param name="p_keyvalue">键值</param>
		private void WriteInfo(string p_keyname,string p_keyvalue)
		{
			RegistryKey SoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);
			RegistryKey CompanyKey=SoftwareKey.CreateSubKey(m_companyname);
			RegistryKey SoftwareNameKey=CompanyKey.CreateSubKey(m_softwarename);
			//写入相应信息
			SoftwareNameKey.SetValue(p_keyname,p_keyvalue);
		}
		/// <summary>
		/// 获取操作员情况,同时更新主程序版本
		/// </summary>
		private void GetInfo()
		{
			this.m_DataSet=new DataSet();
			this.combUsers.Items.Clear();
			string strSql=string.Format("SELECT * FROM  操作员 ORDER BY 姓名");

			//连接数据库
			string strConnection="Provider = Microsoft.Jet.OLEDB.4.0 ;Jet OLEDB:Database Password=;Data Source ="+
				Application.StartupPath.ToString().Trim()+"\\mydatabase.mdb" ;
			OleDbConnection myConnect=new OleDbConnection(strConnection);
			OleDbCommand myCommand=new OleDbCommand(strSql,myConnect);
			OleDbDataAdapter myDataAdapter=new OleDbDataAdapter();
			myDataAdapter.SelectCommand=myCommand;
			try
			{
				myConnect.Open();

				//获取操作员信息
				myDataAdapter.Fill(this.m_DataSet,this.m_TableName);
				//将查询到的用户名填充到组合框供用户选择
				this.m_Table=this.m_DataSet.Tables[this.m_TableName];
				foreach(DataRow row in m_DataSet.Tables[m_TableName].Rows)
				{
					this.combUsers.Items.Add(row["姓名"]).ToString().Trim();
				}

				//检查是否有新的版本
				DataSet dataset=new DataSet();
				string tablename="tablename";
				//为减少数据传送时间,不获取文件内容
				strSql="select 文件名称,版本号 from 版本";
				myCommand=new OleDbCommand(strSql,myConnect);
				myDataAdapter=new OleDbDataAdapter();
				myDataAdapter.SelectCommand=myCommand;
				myDataAdapter.Fill(dataset,tablename);
				if(dataset.Tables[tablename].Rows.Count==1)//有文件
				{
					string filename=dataset.Tables[tablename].Rows[0]["文件名称"].ToString();
					string version=dataset.Tables[tablename].Rows[0]["版本号"].ToString();
					//读入本机主程序的版本号
					string oldversion=this.ReadInfo(filename);
					if(oldversion.Length==0)//不存在
						oldversion="0";
					if(Decimal.Parse(version)>Decimal.Parse(oldversion))//有新的版本出现
					{
						//取回文件内容
						dataset=new DataSet();
						strSql="select * from 版本";
						myCommand=new OleDbCommand(strSql,myConnect);
						myDataAdapter=new OleDbDataAdapter();
						myDataAdapter.SelectCommand=myCommand;
						myDataAdapter.Fill(dataset,tablename);
						//将文件下载到本地
						DataRow row=dataset.Tables[tablename].Rows[0];
						if(row["文件内容"]!=DBNull.Value)
						{

							Byte[] byteBLOBData =  new Byte[0];
							byteBLOBData = (Byte[])row["文件内容"];
							try
							{
								FileStream fs=new FileStream(Application.StartupPath+"\\"+filename,FileMode.OpenOrCreate);
								fs.Write(byteBLOBData,0,byteBLOBData.Length);
								fs.Close();
								//写入当前版本号,供下次使用
								this.WriteInfo(filename,version);
							}
							catch(Exception ee)
							{
								MessageBox.Show(ee.Message);
							}
						}

					}//有新版本
				}//有文件

				//关闭连接
				myConnect.Close();
			}
			catch(Exception ee)
			{
				MessageBox.Show(ee.Message);
				return;
			}
			//允许登录
			this.btnOK.Enabled=true;
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			//为加快显示速度,将数据库连接等放到另外一个线程中去
			Thread thread=new Thread(new ThreadStart(GetInfo));
			thread.Start();
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			//根据组合框的选择,得到当前用户在DataSet中具体物理位置
			if(this.combUsers.SelectedIndex<0)//没有选择
				return;
			DataRow rowNow=null;
			foreach(DataRow row in this.m_DataSet.Tables[this.m_TableName].Rows)
			{
				if(row["姓名"].ToString().Trim()==this.combUsers.Text.Trim())
				{
					rowNow=row;
					break;
				}
			}
			if(rowNow==null)
				return;

			//获取当前正确密码
			string strPassword=rowNow["密码"].ToString().Trim();
			this.txtPassword.Text=this.txtPassword.Text.Trim();
			if(this.txtPassword.Text==strPassword)//密码正确
			{

				//主程序名称
				string filename=Application.StartupPath+"\\"+"MainPro.exe";
				//参数名称
				string arg=this.combUsers.Text+" "+this.txtPassword.Text;
				//运行主程序
				System.Diagnostics.Process fun=System.Diagnostics.Process.Start(filename,arg);

				//关闭登录框
				this.Close();
			}
			else
			{
				MessageBox.Show("    密码错误!如果你确信密码输入正确,\n可以试着检查一下大写字母键是否按下去了。",
					"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
				this.txtPassword.Focus();
				this.txtPassword.SelectAll();
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

	}//form
}

⌨️ 快捷键说明

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