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

📄 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.IO;
namespace UpLoad
//Download by http://www.codefans.net
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtFileName;
		private System.Windows.Forms.TextBox txtVersion;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnBrow;
		private System.Windows.Forms.Label label2;
		/// <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.txtFileName = new System.Windows.Forms.TextBox();
			this.txtVersion = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnBrow = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// txtFileName
			// 
			this.txtFileName.Location = new System.Drawing.Point(24, 48);
			this.txtFileName.Name = "txtFileName";
			this.txtFileName.Size = new System.Drawing.Size(240, 21);
			this.txtFileName.TabIndex = 69;
			this.txtFileName.Text = "D:\\output\\MainPro.exe";
			// 
			// txtVersion
			// 
			this.txtVersion.Location = new System.Drawing.Point(80, 80);
			this.txtVersion.Name = "txtVersion";
			this.txtVersion.Size = new System.Drawing.Size(88, 21);
			this.txtVersion.TabIndex = 68;
			this.txtVersion.Text = "1.0";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.BackColor = System.Drawing.Color.Transparent;
			this.label1.Location = new System.Drawing.Point(24, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(42, 17);
			this.label1.TabIndex = 67;
			this.label1.Text = "文件名";
			// 
			// btnCancel
			// 
			this.btnCancel.BackColor = System.Drawing.SystemColors.Control;
			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.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnCancel.ImageIndex = 1;
			this.btnCancel.Location = new System.Drawing.Point(128, 116);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(72, 32);
			this.btnCancel.TabIndex = 66;
			this.btnCancel.Text = "取消(&C)";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.BackColor = System.Drawing.SystemColors.Control;
			this.btnOK.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnOK.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnOK.ImageIndex = 0;
			this.btnOK.Location = new System.Drawing.Point(32, 116);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(70, 29);
			this.btnOK.TabIndex = 64;
			this.btnOK.Text = "确定(&O)";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnBrow
			// 
			this.btnBrow.BackColor = System.Drawing.SystemColors.Control;
			this.btnBrow.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnBrow.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnBrow.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnBrow.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnBrow.Location = new System.Drawing.Point(80, 16);
			this.btnBrow.Name = "btnBrow";
			this.btnBrow.Size = new System.Drawing.Size(72, 24);
			this.btnBrow.TabIndex = 65;
			this.btnBrow.Text = "浏览(&O)";
			this.btnBrow.Click += new System.EventHandler(this.btnBrow_Click);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.BackColor = System.Drawing.Color.Transparent;
			this.label2.Location = new System.Drawing.Point(32, 88);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(42, 17);
			this.label2.TabIndex = 70;
			this.label2.Text = "版本号";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 181);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtFileName);
			this.Controls.Add(this.txtVersion);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.btnBrow);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private DataSet m_DataSet;
		private string m_TableName="版本";

		private void btnBrow_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog myForm=new OpenFileDialog();
			myForm.Filter="应用程序(*.exe)|*.exe|所有程序(*.*)|*.*";
			if(myForm.ShowDialog()==DialogResult.OK)
			{
				this.txtFileName.Text=myForm.FileName;
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		/// <summary>
		/// 从一个含有路径的文件名中分离出文件名
		/// </summary>
		/// <param name="p_Path">包含路径的文件名</param>
		/// <returns>去掉路径的文件名</returns>
		private string GetFileNameFromPath(string p_Path)
		{
			string strResult="";
			int nStart=p_Path.LastIndexOf("\\");
			if(nStart>0)
			{
				strResult=p_Path.Substring(nStart+1,p_Path.Length-nStart-1);
			}
			return strResult;
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			//检查版本号是否合法
			try
			{
				Decimal.Parse(this.txtVersion.Text);
			}
			catch
			{
				MessageBox.Show("无效的版本号!");
				this.txtVersion.Focus();
				this.txtVersion.SelectAll();
				return;
			}

			if(this.txtFileName.Text.Trim().Length>0)
			{
				//检查文件是否存在
				if(!File.Exists(this.txtFileName.Text.Trim()))
				{
					MessageBox.Show("文件不存在!");
					return;
				}

				//连接数据库
				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("select * from 版本",myConnect);
				OleDbDataAdapter myDataAdapter=new OleDbDataAdapter();
				myDataAdapter.SelectCommand=myCommand;
				OleDbCommandBuilder myCommandBuilder=new OleDbCommandBuilder(myDataAdapter);
				myConnect.Open();

				//获取已有的数据
				m_DataSet=new DataSet();
				try
				{
					myDataAdapter.Fill(m_DataSet,this.m_TableName);
					//如果是首次上传,则增加一条记录
					if(m_DataSet.Tables[m_TableName].Rows.Count==0)
					{
						DataRow newrow=m_DataSet.Tables[m_TableName].NewRow();
						newrow["序号"]="1";
						m_DataSet.Tables[m_TableName].Rows.Add(newrow);
					}
					
					DataRow row=m_DataSet.Tables[m_TableName].Rows[0];
					//填入去掉路径的文件名称
					row["文件名称"]=this.GetFileNameFromPath(this.txtFileName.Text.Trim());
					//填入版本号
					row["版本号"]=this.txtVersion.Text.Trim();

					//将实际文件存入记录中
					FileStream fs=new FileStream(this.txtFileName.Text.Trim(),FileMode.Open);
					byte [] myData = new Byte [fs.Length ];
					fs.Position = 0;
					fs.Read (myData,0,Convert.ToInt32 (fs.Length ));
					row["文件内容"] = myData;
					fs.Close();//关闭文件

					//更新数据库
					myDataAdapter.Update(this.m_DataSet,this.m_TableName);
					myConnect.Close();
					MessageBox.Show("文件更新成功!");
				}
				catch(Exception ee)
				{
					MessageBox.Show(ee.Message);
				}
				
			}
			else
			{
				MessageBox.Show("请输入文件名");
			}
		}
	}//form
}

⌨️ 快捷键说明

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