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(“軟件運行出錯,請重新啓動!”);
}
</div>
</div>