c#,自定义安装,部署,创建自定义操作,自定义操作,安装验证,数据库,安装授权码,接收输入,判断
2008-07-09 12:59 Virus-BeautyCode 阅读(16178) 评论(30) 编辑 收藏 举报
最近做一个项目的安装部署程序,要求有安装的验证,安装的授权,要输入授权吗才可以安装,禁止非法安装。
一开始看见用户界面不错,可是添加了用户界面不能控制他,只能接受输入,然后根据输入创建数据库,修改配置之类的东西,网上的资料也多是这类型的,我就自己写了一个,还不是不太满意的,这些窗体都是在安装的过程中弹出来的,我本意是想在安装之前就验证这些内容,可是弄不出来,不知道大家有没有什么好的办法。
这是需要验证的两个部分,两个自定义的窗体,代码如下:
installer类的代码
一开始看见用户界面不错,可是添加了用户界面不能控制他,只能接受输入,然后根据输入创建数据库,修改配置之类的东西,网上的资料也多是这类型的,我就自己写了一个,还不是不太满意的,这些窗体都是在安装的过程中弹出来的,我本意是想在安装之前就验证这些内容,可是弄不出来,不知道大家有没有什么好的办法。
这是需要验证的两个部分,两个自定义的窗体,代码如下:
installer类的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms;
namespace FactorySetupClassLibrary
{
[RunInstaller(true)]
public partial class Installer1 : Installer
{
private string dbServer;
private string loginUser;
private string password;
private string db;
public Installer1()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
//AuthorizeForm1 authForm = new AuthorizeForm1();
//authForm.ShowDialog();
DBForm1 dbForm = new DBForm1();
if (dbForm.ShowDialog() == DialogResult.OK)
{
}
else
{
throw new ApplicationException("应用程序安装失败");
}
AuthorizeForm1 authForm = new AuthorizeForm1();
if (authForm.ShowDialog() == DialogResult.OK)
{
}
else
{
throw new ApplicationException("应用程序安装失败");
}
}
}
}
数据库验证窗体的代码using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms;
namespace FactorySetupClassLibrary
{
[RunInstaller(true)]
public partial class Installer1 : Installer
{
private string dbServer;
private string loginUser;
private string password;
private string db;
public Installer1()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
//AuthorizeForm1 authForm = new AuthorizeForm1();
//authForm.ShowDialog();
DBForm1 dbForm = new DBForm1();
if (dbForm.ShowDialog() == DialogResult.OK)
{
}
else
{
throw new ApplicationException("应用程序安装失败");
}
AuthorizeForm1 authForm = new AuthorizeForm1();
if (authForm.ShowDialog() == DialogResult.OK)
{
}
else
{
throw new ApplicationException("应用程序安装失败");
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FactorySetupClassLibrary
{
public partial class DBForm1 : Form
{
SqlConnection conn=null;
public DBForm1()
{
InitializeComponent();
}
private void DBForm1_Load(object sender, EventArgs e)
{
txtDB.Text = "KBUdiskFactoryTools";
txtDB.Enabled = false;
txtDBServer.Focus();
}
private void btnOK_Click(object sender, EventArgs e)
{
if (ValidatingHelper.ValidatingTextBox(txtDBServer, 100, "数据库服务器", lblDBServer) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtLoginName, 100, "登录用户名", lblLoginName) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtPassword, 100, "登录密码", lblPassword) == false)
return;
try
{
using (conn = new SqlConnection("server=" + txtDBServer.Text.Trim() + ";uid=" + txtLoginName.Text.Trim() +
";pwd=" + txtPassword.Text.Trim() + ";database=" + txtDB.Text.Trim()))
{
conn.Open();
conn.Close();
this.DialogResult = DialogResult.OK;
}
}
catch
{
if (conn.State != ConnectionState.Closed)
conn.Close();
this.DialogResult = DialogResult.Cancel;
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
private void btnTestConnection_Click(object sender, EventArgs e)
{
if (ValidatingHelper.ValidatingTextBox(txtDBServer, 100, "数据库服务器", lblDBServer) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtLoginName, 100, "登录用户名", lblLoginName) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtPassword, 100, "登录密码", lblPassword) == false)
return;
try
{
using (conn = new SqlConnection("server=" + txtDBServer.Text.Trim() + ";uid=" + txtLoginName.Text.Trim() +
";pwd=" + txtPassword.Text.Trim() + ";database=" + txtDB.Text.Trim()))
{
conn.Open();
conn.Close();
MessageBox.Show("连接成功,可以点击确定按钮继续安装");
}
}
catch
{
if (conn.State != ConnectionState.Closed)
conn.Close();
MessageBox.Show("连接失败,请检查连接设置之后继续安装");
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
授权码验证窗体代码using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FactorySetupClassLibrary
{
public partial class DBForm1 : Form
{
SqlConnection conn=null;
public DBForm1()
{
InitializeComponent();
}
private void DBForm1_Load(object sender, EventArgs e)
{
txtDB.Text = "KBUdiskFactoryTools";
txtDB.Enabled = false;
txtDBServer.Focus();
}
private void btnOK_Click(object sender, EventArgs e)
{
if (ValidatingHelper.ValidatingTextBox(txtDBServer, 100, "数据库服务器", lblDBServer) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtLoginName, 100, "登录用户名", lblLoginName) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtPassword, 100, "登录密码", lblPassword) == false)
return;
try
{
using (conn = new SqlConnection("server=" + txtDBServer.Text.Trim() + ";uid=" + txtLoginName.Text.Trim() +
";pwd=" + txtPassword.Text.Trim() + ";database=" + txtDB.Text.Trim()))
{
conn.Open();
conn.Close();
this.DialogResult = DialogResult.OK;
}
}
catch
{
if (conn.State != ConnectionState.Closed)
conn.Close();
this.DialogResult = DialogResult.Cancel;
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
private void btnTestConnection_Click(object sender, EventArgs e)
{
if (ValidatingHelper.ValidatingTextBox(txtDBServer, 100, "数据库服务器", lblDBServer) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtLoginName, 100, "登录用户名", lblLoginName) == false)
return;
if (ValidatingHelper.ValidatingTextBox(txtPassword, 100, "登录密码", lblPassword) == false)
return;
try
{
using (conn = new SqlConnection("server=" + txtDBServer.Text.Trim() + ";uid=" + txtLoginName.Text.Trim() +
";pwd=" + txtPassword.Text.Trim() + ";database=" + txtDB.Text.Trim()))
{
conn.Open();
conn.Close();
MessageBox.Show("连接成功,可以点击确定按钮继续安装");
}
}
catch
{
if (conn.State != ConnectionState.Closed)
conn.Close();
MessageBox.Show("连接失败,请检查连接设置之后继续安装");
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FactorySetupClassLibrary
{
public partial class AuthorizeForm1 : Form
{
private Hasher _hasher = null;
private int _randomInt;
public AuthorizeForm1()
{
InitializeComponent();
_hasher = new Hasher();
}
private void AuthorizeForm1_Load(object sender, EventArgs e)
{
Random random = new Random();
_randomInt = random.Next(9999, 99999);
//this._hasher.HashText = this._computerInfo.GetCPUSn() + this._computerInfo.GetHardDriveSn() +
// this._computerInfo.GetMacAddress() + this._computerInfo.GetMainBoardId() + this._randomInt.ToString();
this._hasher.HashText = ComputerInfo.GetManchineCPU() + ComputerInfo.GetMachineHardDiskNumber() +
ComputerInfo.GetMachineMac() + ComputerInfo.GetMainBoardId() + this._randomInt.ToString();
txtApplyCode.Text = this._hasher.MD5Hasher();
txtApplyCode.Enabled = false;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (txtAuthorizeCode.Text == "")
{
errorProvider1.SetError(lblAuthCode, "请输入授权码");
txtAuthorizeCode.Focus();
return;
}
else if (txtAuthorizeCode.TextLength != 24)
{
errorProvider1.SetError(lblAuthCode, "请输入24位的授权码");
txtAuthorizeCode.SelectAll();
txtAuthorizeCode.Focus();
return;
}
else
{
errorProvider1.SetError(lblAuthCode, "");
}
//this._hasher.HashText = txtApplyCode.Text.Trim();
Hasher hasher = new Hasher();
hasher.HashText = txtApplyCode.Text.Trim();
//MessageBox.Show("申请码:"+txtApplyCode.Text.Trim()+"授权码:"+hasher.MD5Hasher()+"输入的授权码:"+txtAuthorizeCode.Text.Trim());
if (hasher.MD5Hasher().Equals(txtAuthorizeCode.Text.Trim()))
this.DialogResult = DialogResult.OK;
else
this.DialogResult = DialogResult.Cancel;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
两个帮助类,一个是加密类,一个是输入验证类
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FactorySetupClassLibrary
{
public partial class AuthorizeForm1 : Form
{
private Hasher _hasher = null;
private int _randomInt;
public AuthorizeForm1()
{
InitializeComponent();
_hasher = new Hasher();
}
private void AuthorizeForm1_Load(object sender, EventArgs e)
{
Random random = new Random();
_randomInt = random.Next(9999, 99999);
//this._hasher.HashText = this._computerInfo.GetCPUSn() + this._computerInfo.GetHardDriveSn() +
// this._computerInfo.GetMacAddress() + this._computerInfo.GetMainBoardId() + this._randomInt.ToString();
this._hasher.HashText = ComputerInfo.GetManchineCPU() + ComputerInfo.GetMachineHardDiskNumber() +
ComputerInfo.GetMachineMac() + ComputerInfo.GetMainBoardId() + this._randomInt.ToString();
txtApplyCode.Text = this._hasher.MD5Hasher();
txtApplyCode.Enabled = false;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (txtAuthorizeCode.Text == "")
{
errorProvider1.SetError(lblAuthCode, "请输入授权码");
txtAuthorizeCode.Focus();
return;
}
else if (txtAuthorizeCode.TextLength != 24)
{
errorProvider1.SetError(lblAuthCode, "请输入24位的授权码");
txtAuthorizeCode.SelectAll();
txtAuthorizeCode.Focus();
return;
}
else
{
errorProvider1.SetError(lblAuthCode, "");
}
//this._hasher.HashText = txtApplyCode.Text.Trim();
Hasher hasher = new Hasher();
hasher.HashText = txtApplyCode.Text.Trim();
//MessageBox.Show("申请码:"+txtApplyCode.Text.Trim()+"授权码:"+hasher.MD5Hasher()+"输入的授权码:"+txtAuthorizeCode.Text.Trim());
if (hasher.MD5Hasher().Equals(txtAuthorizeCode.Text.Trim()))
this.DialogResult = DialogResult.OK;
else
this.DialogResult = DialogResult.Cancel;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
两个帮助类,一个是加密类,一个是输入验证类
using System;
using System.IO;
using System.Security.Cryptography;
namespace FactorySetupClassLibrary
{
/**/
/// <summary>
///Copyright (C), 2004, kwklover(邝伟科)
///File name:Hasher.cs
///Author:邝伟科 Version:1.0 Date:2004年4月22日
///Description:哈希(不可逆)加密通用类库函数
/// </summary>
public class Hasher
{
private byte[] _HashKey; //哈希密钥存储变量
private string _HashText; //待加密的字符串
public Hasher()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 带参数构造函数
/// </summary>
/// <param name="hashText">待加密的字符串</param>
public Hasher(string hashText)
{
this._HashText = hashText;
}
/**/
/// <summary>
/// 哈希密钥
/// </summary>
public byte[] HashKey
{
set
{
_HashKey = value;
}
get
{
return _HashKey;
}
}
/**/
/// <summary>
/// 需要产生加密哈希的字符串
/// </summary>
public string HashText
{
set
{
_HashText = value;
}
get
{
return _HashText;
}
}
/**/
/// <summary>
/// 使用HMACSHA1类产生长度为 20 字节的哈希序列。需提供相应的密钥,接受任何大小的密钥。
/// </summary>
/// <returns></returns>
public string HMACSHA1Hasher()
{
byte[] HmacKey = HashKey;
byte[] HmacData = System.Text.Encoding.UTF8.GetBytes(HashText);
HMACSHA1 Hmac = new HMACSHA1(HmacKey);
CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write);
cs.Write(HmacData, 0, HmacData.Length);
cs.Close();
byte[] Result = Hmac.Hash;
return Convert.ToBase64String(Result); //返回长度为28字节字符串
}
/**/
/// <summary>
/// 使用MACTripleDES类产生长度为 8 字节的哈希序列。需提供相应的密钥,密钥长度可为 8、16 或 24 字节的密钥。
/// </summary>
/// <returns></returns>
public string MACTripleDESHasher()
{
byte[] MacKey = HashKey;
byte[] MacData = System.Text.Encoding.UTF8.GetBytes(HashText);
MACTripleDES Mac = new MACTripleDES(MacKey);
byte[] Result = Mac.ComputeHash(MacData);
return Convert.ToBase64String(Result); //返回长度为12字节字符串
}
/**/
/// <summary>
/// 使用MD5CryptoServiceProvider类产生哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string MD5Hasher()
{
byte[] MD5Data = System.Text.Encoding.UTF8.GetBytes(HashText);
MD5 Md5 = new MD5CryptoServiceProvider();
byte[] Result = Md5.ComputeHash(MD5Data);
return Convert.ToBase64String(Result); //返回长度为25字节字符串
}
/**/
/// <summary>
/// 使用SHA1Managed类产生长度为160位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA1ManagedHasher()
{
byte[] SHA1Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA1Managed Sha1 = new SHA1Managed();
byte[] Result = Sha1.ComputeHash(SHA1Data);
return Convert.ToBase64String(Result); //返回长度为28字节的字符串
}
/**/
/// <summary>
/// 使用SHA256Managed类产生长度为256位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA256ManagedHasher()
{
byte[] SHA256Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
/**/
/// <summary>
/// 使用SHA384Managed类产生长度为384位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA384ManagedHasher()
{
byte[] SHA384Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA384Managed Sha384 = new SHA384Managed();
byte[] Result = Sha384.ComputeHash(SHA384Data);
return Convert.ToBase64String(Result); //返回长度为64字节的字符串
}
/**/
/// <summary>
/// 使用SHA512Managed类产生长度为512位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA512ManagedHasher()
{
byte[] SHA512Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA512Managed Sha512 = new SHA512Managed();
byte[] Result = Sha512.ComputeHash(SHA512Data);
return Convert.ToBase64String(Result); //返回长度为88字节的字符串
}
}
}
文本框输入内容验证类
using System.IO;
using System.Security.Cryptography;
namespace FactorySetupClassLibrary
{
/**/
/// <summary>
///Copyright (C), 2004, kwklover(邝伟科)
///File name:Hasher.cs
///Author:邝伟科 Version:1.0 Date:2004年4月22日
///Description:哈希(不可逆)加密通用类库函数
/// </summary>
public class Hasher
{
private byte[] _HashKey; //哈希密钥存储变量
private string _HashText; //待加密的字符串
public Hasher()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 带参数构造函数
/// </summary>
/// <param name="hashText">待加密的字符串</param>
public Hasher(string hashText)
{
this._HashText = hashText;
}
/**/
/// <summary>
/// 哈希密钥
/// </summary>
public byte[] HashKey
{
set
{
_HashKey = value;
}
get
{
return _HashKey;
}
}
/**/
/// <summary>
/// 需要产生加密哈希的字符串
/// </summary>
public string HashText
{
set
{
_HashText = value;
}
get
{
return _HashText;
}
}
/**/
/// <summary>
/// 使用HMACSHA1类产生长度为 20 字节的哈希序列。需提供相应的密钥,接受任何大小的密钥。
/// </summary>
/// <returns></returns>
public string HMACSHA1Hasher()
{
byte[] HmacKey = HashKey;
byte[] HmacData = System.Text.Encoding.UTF8.GetBytes(HashText);
HMACSHA1 Hmac = new HMACSHA1(HmacKey);
CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write);
cs.Write(HmacData, 0, HmacData.Length);
cs.Close();
byte[] Result = Hmac.Hash;
return Convert.ToBase64String(Result); //返回长度为28字节字符串
}
/**/
/// <summary>
/// 使用MACTripleDES类产生长度为 8 字节的哈希序列。需提供相应的密钥,密钥长度可为 8、16 或 24 字节的密钥。
/// </summary>
/// <returns></returns>
public string MACTripleDESHasher()
{
byte[] MacKey = HashKey;
byte[] MacData = System.Text.Encoding.UTF8.GetBytes(HashText);
MACTripleDES Mac = new MACTripleDES(MacKey);
byte[] Result = Mac.ComputeHash(MacData);
return Convert.ToBase64String(Result); //返回长度为12字节字符串
}
/**/
/// <summary>
/// 使用MD5CryptoServiceProvider类产生哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string MD5Hasher()
{
byte[] MD5Data = System.Text.Encoding.UTF8.GetBytes(HashText);
MD5 Md5 = new MD5CryptoServiceProvider();
byte[] Result = Md5.ComputeHash(MD5Data);
return Convert.ToBase64String(Result); //返回长度为25字节字符串
}
/**/
/// <summary>
/// 使用SHA1Managed类产生长度为160位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA1ManagedHasher()
{
byte[] SHA1Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA1Managed Sha1 = new SHA1Managed();
byte[] Result = Sha1.ComputeHash(SHA1Data);
return Convert.ToBase64String(Result); //返回长度为28字节的字符串
}
/**/
/// <summary>
/// 使用SHA256Managed类产生长度为256位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA256ManagedHasher()
{
byte[] SHA256Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
/**/
/// <summary>
/// 使用SHA384Managed类产生长度为384位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA384ManagedHasher()
{
byte[] SHA384Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA384Managed Sha384 = new SHA384Managed();
byte[] Result = Sha384.ComputeHash(SHA384Data);
return Convert.ToBase64String(Result); //返回长度为64字节的字符串
}
/**/
/// <summary>
/// 使用SHA512Managed类产生长度为512位哈希值。不需要提供密钥。
/// </summary>
/// <returns></returns>
public string SHA512ManagedHasher()
{
byte[] SHA512Data = System.Text.Encoding.UTF8.GetBytes(HashText);
SHA512Managed Sha512 = new SHA512Managed();
byte[] Result = Sha512.ComputeHash(SHA512Data);
return Convert.ToBase64String(Result); //返回长度为88字节的字符串
}
}
}
文本框输入内容验证类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace FactorySetupClassLibrary
{
/// <summary>
/// 输入验证帮助类
/// </summary>
public class ValidatingHelper
{
/// <summary>
/// 验证输入文本框控件
/// </summary>
/// <param name="validatedControl">需要验证的控件</param>
/// <param name="length">输入长度</param>
/// <param name="msg">文本框的验证内容</param>
/// <param name="msgControl">显示错误信息的控件</param>
/// <returns></returns>
public static bool ValidatingTextBox(Control validatedControl,int length, string msg, Control msgControl)
{
ErrorProvider error = new ErrorProvider();
if (validatedControl.Text == "")
{
error.SetError(msgControl, "请输入" + msg);
validatedControl.Focus();
return false;
}
else
{
error.SetError(msgControl, "");
return true;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace FactorySetupClassLibrary
{
/// <summary>
/// 输入验证帮助类
/// </summary>
public class ValidatingHelper
{
/// <summary>
/// 验证输入文本框控件
/// </summary>
/// <param name="validatedControl">需要验证的控件</param>
/// <param name="length">输入长度</param>
/// <param name="msg">文本框的验证内容</param>
/// <param name="msgControl">显示错误信息的控件</param>
/// <returns></returns>
public static bool ValidatingTextBox(Control validatedControl,int length, string msg, Control msgControl)
{
ErrorProvider error = new ErrorProvider();
if (validatedControl.Text == "")
{
error.SetError(msgControl, "请输入" + msg);
validatedControl.Focus();
return false;
}
else
{
error.SetError(msgControl, "");
return true;
}
}
}
}