?? form1.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 + -