您的位置:首页 > 编程语言 > C#

C#实现注册码功能编程总结

2016-03-19 18:28 477 查看

C# 给自己写的软件,加注册码功能

  为自己写的程序加一个注册功能吧。生成的机器号是根据CPU和硬盘号来的,根据自己的需求改成是否是随机生成。

  代码直接粘贴到新建类覆盖原代码就能直接用了。

using System;

using System.Management;

using System.Security.Cryptography;

using System.Text;

namespace RegisterClass

{

class RegisterClass

{

//步骤一: 获得CUP序列号和硬盘序列号的实现代码如下:

//获得CPU的序列号

bool Stupids = true;

bool Cat = false;

public string getCpu()

{

string strCpu = null;

ManagementClass myCpu = new ManagementClass("win32_Processor");

ManagementObjectCollection myCpuConnection = myCpu.GetInstances();

foreach( ManagementObject myObject in myCpuConnection)

{

strCpu = myObject.Properties["Processorid"].Value.ToString();

break;

}

return strCpu;

}

//取得设备硬盘的卷标号

public string GetDiskVolumeSerialNumber()

{

ManagementClass mc =

new ManagementClass("Win32_NetworkAdapterConfiguration");

ManagementObject disk =

new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");

disk.Get();

return disk.GetPropertyValue("VolumeSerialNumber").ToString();

}

//步骤二: 收集硬件信息生成机器码, 代码如下:

//生成机器码

public string CreateCode()

{

string temp = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号

string[] strid = new string[24];//

for (int i = 0; i < 24; i++)//把字符赋给数组

{

strid[i] = temp.Substring(i, 1);

}

temp = "";

//Random rdid = new Random();

for (int i = 0; i < 24; i++)//从数组随机抽取24个字符组成新的字符生成机器三

{

//temp += strid[rdid.Next(0, 24)];

temp += strid[i+3>=24?0:i+3];

}

return GetMd5(temp);

}

//步骤三: 使用机器码生成软件注册码, 代码如下:

//使用机器码生成注册码

public int[] intCode = new int[127];//用于存密钥

public void setIntCode()//给数组赋值个小于10的随机数

{

//Random ra = new Random();

//for (int i = 1; i < intCode.Length;i++ )

//{

// intCode[i] = ra.Next(0, 9);

//}

for (int i = 1; i < intCode.Length; i++)

{

intCode[i] = i + 3 > 9 ? 0 : i + 3;

}

}

public int[] intNumber = new int[25];//用于存机器码的Ascii值

public char[] Charcode = new char[25];//存储机器码字

//生成注册码

public string GetCode(string code)

{

if (code != "")

{

//把机器码存入数组中

setIntCode();//初始化127位数组

for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中

{

Charcode[i] = Convert.ToChar(code.Substring(i - 1, 1));

}//

for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。

{

intNumber[j] =

intCode[Convert.ToInt32(Charcode[j])] +

Convert.ToInt32(Charcode[j]);

}

string strAsciiName = null;//用于存储机器码

for (int j = 1; j < intNumber.Length; j++)

{

//MessageBox.Show((Convert.ToChar(intNumber[j])).ToString());

//判断字符ASCII值是否0-9之间

if (intNumber[j] >= 48 && intNumber[j] <= 57)

{

strAsciiName += Convert.ToChar(intNumber[j]).ToString();

}

//判断字符ASCII值是否A-Z之间

else if (intNumber[j] >= 65 && intNumber[j] <= 90)

{

strAsciiName += Convert.ToChar(intNumber[j]).ToString();

}

//判断字符ASCII值是否a-z之间

else if (intNumber[j] >= 97 && intNumber[j] <= 122)

{

strAsciiName += Convert.ToChar(intNumber[j]).ToString();

}

else//判断字符ASCII值不在以上范围内

{

if (intNumber[j] > 122)//判断字符ASCII值是否大于z

{

strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();

}

else

{

strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();

}

}

//label3.Text = strAsciiName;//得到注册码

}

return strAsciiName;

}

else

{

return "";

}

}

//步骤四: 用户输入注册码注册软件, 演示代码如下:

//注册

public bool RegistIt(string currentCode,string realCode)

{

if (realCode != "")

{

if (currentCode.TrimEnd().Equals(realCode.TrimEnd()))

{

Microsoft.Win32.RegistryKey retkey =

Microsoft.Win32.Registry.CurrentUser.

OpenSubKey("software", true).CreateSubKey("StupidsCat").

CreateSubKey("StupidsCat.ini").

CreateSubKey(currentCode.TrimEnd());

retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");

retkey = Microsoft.Win32.Registry.LocalMachine.

OpenSubKey("software", true).CreateSubKey("StupidsCat").

CreateSubKey("StupidsCat.ini").

CreateSubKey(currentCode.TrimEnd());

retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");

return Stupids;

}

else

{

return Cat;

}

}

else { return Cat; }

}

public bool BoolRegist(string sn)

{

string[] keynames; bool flag = false;

Microsoft.Win32.RegistryKey localRegKey = Microsoft.Win32.Registry.LocalMachine;

Microsoft.Win32.RegistryKey userRegKey = Microsoft.Win32.Registry.CurrentUser;

try

{

keynames = localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();

foreach (string name in keynames)

{

if (name == "StupidsCat")

{

if (localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")

flag = true;

}

}

keynames = userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();

foreach (string name in keynames)

{

if (name == "StupidsCat")

{

if (flag && userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")

return true;

}

}

return false;

}

catch

{

return false;

}

finally

{

localRegKey.Close();

userRegKey.Close();

}

}

public string GetMd5(object text)

{

string path = text.ToString();

MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider();

Byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(text.ToString());

Byte[] byteResult = MD5Pro.ComputeHash(buffer);

string md5result = BitConverter.ToString(byteResult).Replace("-", "");

return md5result;

}

}

}

========

C#学习笔记——软件注册与注册机

SoftReg类:

1: using System;

2: using System.Collections.Generic;

3: using System.Linq;

4: using System.Text;

5: using System.Management; //需要引用System.Management.dll

6:

7: namespace SoftRegister

8: {

9: class SoftReg

10: {

11: ///<summary>

12: /// 获取硬盘卷标号

13: ///</summary>

14: ///<returns></returns>

15: public string GetDiskVolumeSerialNumber()

16: {

17: ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration");

18: ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");

19: disk.Get();

20: return disk.GetPropertyValue("VolumeSerialNumber").ToString();

21: }

22:

23: ///<summary>

24: /// 获取CPU序列号

25: ///</summary>

26: ///<returns></returns>

27: public string GetCpu()

28: {

29: string strCpu = null;

30: ManagementClass myCpu = new ManagementClass("win32_Processor");

31: ManagementObjectCollection myCpuCollection = myCpu.GetInstances();

32: foreach (ManagementObject myObject in myCpuCollection)

33: {

34: strCpu = myObject.Properties["Processorid"].Value.ToString();

35: }

36: return strCpu;

37: }

38:

39: ///<summary>

40: /// 生成机器码

41: ///</summary>

42: ///<returns></returns>

43: public string GetMNum()

44: {

45: string strNum = GetCpu() + GetDiskVolumeSerialNumber();

46: string strMNum = strNum.Substring(0, 24); //截取前24位作为机器码

47: return strMNum;

48: }

49:

50: public int[] intCode = new int[127]; //存储密钥

51: public char[] charCode = new char[25]; //存储ASCII码

52: public int[] intNumber = new int[25]; //存储ASCII码值

53:

54: //初始化密钥

55: public void SetIntCode()

56: {

57: for (int i = 1; i < intCode.Length; i++)

58: {

59: intCode[i] = i % 9;

60: }

61: }

62:

63: ///<summary>

64: /// 生成注册码

65: ///</summary>

66: ///<returns></returns>

67: public string GetRNum()

68: {

69: SetIntCode();

70: string strMNum = GetMNum();

71: for (int i = 1; i < charCode.Length; i++) //存储机器码

72: {

73: charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));

74: }

75: for (int j = 1; j < intNumber.Length; j++) //改变ASCII码值

76: {

77: intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])];

78: }

79: string strAsciiName = ""; //注册码

80: for (int k = 1; k < intNumber.Length; k++) //生成注册码

81: {

82:

83: if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k]

84: <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) //判断如果在0-9、A-Z、a-z之间

85: {

86: strAsciiName += Convert.ToChar(intNumber[k]).ToString();

87: }

88: else if (intNumber[k] > 122) //判断如果大于z

89: {

90: strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString();

91: }

92: else

93: {

94: strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString();

95: }

96: }

97: return strAsciiName;

98: }

99: }

100: }

主窗体:

image

1: using System;

2: using System.Collections.Generic;

3: using System.ComponentModel;

4: using System.Data;

5: using System.Drawing;

6: using System.Linq;

7: using System.Text;

8: using System.Windows.Forms;

9: using Microsoft.Win32;

10:

11: namespace SoftRegister

12: {

13: public partial class FormMain : Form

14: {

15: public FormMain()

16: {

17: InitializeComponent();

18: }

19:

20: SoftReg softReg = new SoftReg();

21:

22: private void FormMain_Load(object sender, EventArgs e)

23: {

24: //判断软件是否注册

25: RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");

26: foreach (string strRNum in retkey.GetSubKeyNames())

27: {

28: if (strRNum == softReg.GetRNum())

29: {

30: this.labRegInfo.Text = "此软件已注册!";

31: this.btnReg.Enabled = false;

32: return;

33: }

34: }

35: this.labRegInfo.Text = "此软件尚未注册!";

36: this.btnReg.Enabled = true;

37: MessageBox.Show("您现在使用的是试用版,可以免费试用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

38:

39: Int32 tLong; //已使用次数

40: try

41: {

42: tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);

43: MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

44: }

45: catch

46: {

47: MessageBox.Show("欢迎使用本软件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

48: Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0, RegistryValueKind.DWord);

49: }

50:

51: //判断是否可以继续试用

52: tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);

53: if (tLong < 30)

54: {

55: int tTimes = tLong + 1;

56: Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", tTimes);

57: }

58: else

59: {

60: DialogResult result = MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

61: if (result == DialogResult.Yes)

62: {

63: FormRegister.state = false; //设置软件状态为不可用

64: btnReg_Click(sender, e); //打开注册窗口

65: }

66: else

67: {

68: Application.Exit();

69: }

70: }

71:

72: }

73:

74: private void btnClose_Click(object sender, EventArgs e)

75: {

76: Application.Exit();

77: }

78:

79: private void btnReg_Click(object sender, EventArgs e)

80: {

81: FormRegister frmRegister = new FormRegister();

82: frmRegister.ShowDialog();

83: }

84: }

85: }

注册窗体:

image

1: using System;

2: using System.Collections.Generic;

3: using System.ComponentModel;

4: using System.Data;

5: using System.Drawing;

6: using System.Linq;

7: using System.Text;

8: using System.Windows.Forms;

9: using Microsoft.Win32;

10:

11: namespace SoftRegister

12: {

13: public partial class FormRegister : Form

14: {

15: public FormRegister()

16: {

17: InitializeComponent();

18: }

19:

20: public static bool state = true; //软件是否为可用状态

21: SoftReg softReg = new SoftReg();

22:

23: private void btnReg_Click(object sender, EventArgs e)

24: {

25: try

26: {

27: if (txtLicence.Text == softReg.GetRNum())

28: {

29: MessageBox.Show("注册成功!重启软件后生效!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

30: RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI").CreateSubKey(txtLicence.Text);

31: retkey.SetValue("UserName", "Rsoft");

32: this.Close();

33: }

34: else

35: {

36: MessageBox.Show("注册码错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

37: txtLicence.SelectAll();

38: }

39: }

40: catch (Exception ex)

41: {

42: throw new Exception(ex.Message);

43: }

44: }

45:

46: private void btnClose_Click(object sender, EventArgs e)

47: {

48: if (state == true)

49: {

50: this.Close();

51: }

52: else

53: {

54: Application.Exit();

55: }

56: }

57:

58: private void FormRegister_Load(object sender, EventArgs e)

59: {

60: this.txtHardware.Text = softReg.GetMNum();

61: }

62: }

63: }

(二)注册机的实现:

SoftReg类:

1: using System;

2: using System.Collections.Generic;

3: using System.Linq;

4: using System.Text;

5: using System.Management;

6:

7: namespace SoftwarePassport

8: {

9: class SoftReg

10: {

11: public int[] intCode = new int[127]; //存储密钥

12: public char[] charCode = new char[25]; //存储ASCII码

13: public int[] intNumber = new int[25]; //存储ASCII码值

14:

15: //初始化密钥

16: public void SetIntCode()

17: {

18: for (int i = 1; i < intCode.Length; i++)

19: {

20: intCode[i] = i % 9;

21: }

22: }

23:

24: ///<summary>

25: /// 生成注册码

26: ///</summary>

27: ///<returns></returns>

28: public string GetRNum(string strMNum)

29: {

30: SetIntCode();

31:

32: for (int i = 1; i < charCode.Length; i++) //存储机器码

33: {

34: charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));

35: }

36: for (int j = 1; j < intNumber.Length; j++) //改变ASCII码值

37: {

38: intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])];

39: }

40: string strAsciiName = ""; //注册码

41: for (int k = 1; k < intNumber.Length; k++) //生成注册码

42: {

43:

44: if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k]

45: <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) //判断如果在0-9、A-Z、a-z之间

46: {

47: strAsciiName += Convert.ToChar(intNumber[k]).ToString();

48: }

49: else if (intNumber[k] > 122) //判断如果大于z

50: {

51: strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString();

52: }

53: else

54: {

55: strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString();

56: }

57: }

58: return strAsciiName;

59: }

60: }

61: }

主窗体:

image

1: using System;

2: using System.Collections.Generic;

3: using System.ComponentModel;

4: using System.Data;

5: using System.Drawing;

6: using System.Linq;

7: using System.Text;

8: using System.Windows.Forms;

9:

10: namespace SoftwarePassport

11: {

12: public partial class FormMain : Form

13: {

14: public FormMain()

15: {

16: InitializeComponent();

17: }

18:

19: SoftReg softReg = new SoftReg();

20:

21: private void btnCreate_Click(object sender, EventArgs e)

22: {

23: try

24: {

25: string strHardware = this.txtHardware.Text;

26: string strLicence = softReg.GetRNum(strHardware);

27: this.txtLicence.Text = strLicence;

28: }

29: catch (System.Exception ex)

30: {

31: MessageBox.Show("输入的机器码格式错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

32: }

33: }

34:

35: private void btnExit_Click(object sender, EventArgs e)

36: {

37: Application.Exit();

38: }

39: }

40: }

========

C#软件加序列号激活、试用期限

程序员想尽各种办法给软件加密,用户就想尽各种办法对软件破解。

现在做软件试用限制,那么就讨论下软件的试用限制。总体来说,限制的方法有这么几种:

1.时间限制。

2.次数限制。

以时间限制为例,主要是用户从安装之日起, 限制用户使用天数。n天之后,就无法使用。这种限制主要是安装的时候,将当前日期写入注册表(或者硬盘上某文件)。当然,写入的是加密过的乱码字符。运行软件时,首先读取注册表(或者文件),如找不到

注册表(或者文件),则提示软件未注册。当正常读取后进行解密,得到注册日期,与当前日期进行比较,如果 当前日期 减去 注册日期 > n(允许试用天数),那么提示软件试用到期,直接退出软件。否则 提示可试用天数, 继续试用软件。 根据以上思路,那么

用户可以很容易破解软件。比如更改系统日期、或者删除注册表,重新安装软件等 。

针对用户的破解,对软件限制进行修改。如果试用软件必须联网,或者需要服务器端(比如聊天软件等客户端软件),当前时间要从去服务器的时间,防止用户更改客户机系统时间。或者服务器上对客户机进行记录,如记录主板id,安装时间,等等。。。

以上为客户机可联网的做法,当客户机无法上网,切不存在服务器,或者服务器就在本机时。以上做法将无法使用。

那么对于单机运行的软件,如果需要数据库,我们可以将注册时间等信息写入数据库。或者,我们可以采用一明一暗的做法,注册表是明,在硬盘的某角落,存放隐藏文件。软件需读取两处,对两处进行比较,一致则通过,不一致就退出程序。当然,安装的时候

对该文件不替换。 我想用户是不愿意为了使用你的软件而格式化整个硬盘的。

其实还有做法,就是每次运行软件,先将当前日期与注册表对比,看是否过期。如未过期,就对注册表进行一次更改,更改为当前日期,那么用户即使更改系统日期,他的试用期限也在逐渐缩小。为了防止用户重装,还是采用一明一暗的做法。

基本上就这些方法吧.. 贴上测试代码:

加密解密类:

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Xml.Linq;

using System.IO;

using System.Text;

using System.Security.Cryptography;

namespace Add_To_Regedit

{

public class Encryption

{

public static string EncryPW(string Pass, string Key)

{

return DesEncrypt(Pass, Key);

}

public static string DisEncryPW(string strPass, string Key)

{

return DesDecrypt(strPass, Key);

}

/////////////////////////////////////////////////////////////////////

/// <summary>

/// DES加密

/// </summary>

/// <param name="encryptString"></param>

/// <returns></returns>

public static string DesEncrypt(string encryptString, string key)

{

byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

byte[] keyIV = keyBytes;

byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();

CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);

cStream.Write(inputByteArray, 0, inputByteArray.Length);

cStream.FlushFinalBlock();

return Convert.ToBase64String(mStream.ToArray());

}

/// <summary>

/// DES解密

/// </summary>

/// <param name="decryptString"></param>

/// <returns></returns>

public static string DesDecrypt(string decryptString, string key)

{

byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

byte[] keyIV = keyBytes;

byte[] inputByteArray = Convert.FromBase64String(decryptString);

DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();

CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);

cStream.Write(inputByteArray, 0, inputByteArray.Length);

cStream.FlushFinalBlock();

return Encoding.UTF8.GetString(mStream.ToArray());

}

//////////////////////////////////////////////////////

}

}

读写注册表类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Management;

using System.Security.Cryptography;

using Microsoft.Win32;

namespace Test_Form_Time

{

class TimeClass

{

public static int InitRegedit()

{

/*检查注册表*/

string SericalNumber = ReadSetting("", "SerialNumber", "-1"); // 读取注册表, 检查是否注册 -1为未注册

if (SericalNumber == "-1")

{

return 1;

}

/* 比较CPUid */

string CpuId = GetSoftEndDateAllCpuId(1, SericalNumber); //从注册表读取CPUid

string CpuIdThis = GetCpuId(); //获取本机CPUId

if (CpuId != CpuIdThis)

{

return 2;

}

/* 比较时间 */

string NowDate = TimeClass.GetNowDate();

string EndDate = TimeClass.GetSoftEndDateAllCpuId(0, SericalNumber);

if (Convert.ToInt32(EndDate) - Convert.ToInt32(NowDate) < 0)

{

return 3;

}

return 0;

}

/*CPUid*/

public static string GetCpuId()

{

ManagementClass mc = new ManagementClass("Win32_Processor");

ManagementObjectCollection moc = mc.GetInstances();

string strCpuID = null;

foreach (ManagementObject mo in moc)

{

strCpuID = mo.Properties["ProcessorId"].Value.ToString();

break;

}

return strCpuID;

}

/*当前时间*/

public static string GetNowDate()

{

string NowDate = DateTime.Now.ToString("yyyyMMdd"); //.Year + DateTime.Now.Month + DateTime.Now.Day).ToString();

// DateTime date = Convert.ToDateTime(NowDate, "yyyy/MM/dd");

return NowDate;

}

/* 生成序列号 */

public static string CreatSerialNumber()

{

string SerialNumber = GetCpuId() + "-" + "20110915";

return SerialNumber;

}

/*

* i=1 得到 CUP 的id

* i=0 得到上次或者 开始时间

*/

public static string GetSoftEndDateAllCpuId(int i, string SerialNumber)

{

if (i == 1)

{

string cupId = SerialNumber.Substring(0, SerialNumber.LastIndexOf("-")); // .LastIndexOf("-"));

return cupId;

}

if (i == 0)

{

string dateTime = SerialNumber.Substring(SerialNumber.LastIndexOf("-") + 1);

// dateTime = dateTime.Insert(4, "/").Insert(7, "/");

// DateTime date = Convert.ToDateTime(dateTime);

return dateTime;

}

else

{

return string.Empty;

}

}

/*写入注册表*/

public static void WriteSetting(string Section, string Key, string Setting) // name = key value=setting Section= path

{

string text1 = Section;

RegistryKey key1 = Registry.CurrentUser.CreateSubKey("Software\\MyTest_ChildPlat\\ChildPlat"); // .LocalMachine.CreateSubKey("Software\\mytest");

if (key1 == null)

{

return;

}

try

{

key1.SetValue(Key, Setting);

}

catch (Exception exception1)

{

return;

}

finally

{

key1.Close();

}

}

/*读取注册表*/

public static string ReadSetting(string Section, string Key, string Default)

{

if (Default == null)

{

Default = "-1";

}

string text2 = Section;

RegistryKey key1 = Registry.CurrentUser.OpenSubKey("Software\\MyTest_ChildPlat\\ChildPlat");

if (key1 != null)

{

object obj1 = key1.GetValue(Key, Default);

key1.Close();

if (obj1 != null)

{

if (!(obj1 is string))

{

return "-1";

}

string obj2 = obj1.ToString();

obj2 = Encryption.DisEncryPW(obj2, "ejiang11");

return obj2;

}

return "-1";

}

return Default;

}

}

}

调用方式如下:

int res = TimeClass.InitRegedit();

if (res == 0)

{

Application.Run(new Form1());

}

else if(res == 1)

{

MessageBox.Show("软件尚未注册,请注册软件!");

}

else if (res == 2)

{

MessageBox.Show("注册机器与本机不一致,请联系管理员!");

}

else if (res == 3)

{

MessageBox.Show("软件试用已到期!");

}

else

{

MessageBox.Show("软件运行出错,请重新启动!");

}

========
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: