软件添加注册功能: 给用户一个界面输入Key(注:此Key是我用别的工具加密生成), 直接写到用户电脑的注册表中。软件每次开启,都会对比注册表中的时间与当前时间,以此判断软件是否过期,
代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
/*写入注册表*/ 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\\efwplusServer\\cdkey"); // .LocalMachine.CreateSubKey("Software\\mytest"); if (key1 == null) { return; } try { key1.SetValue(Key, Setting); } catch (Exception exception1) { return; } finally { key1.Close(); } }
/// <summary> /// 读取注册表 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Default"></param> /// <returns></returns> public static string ReadSetting(string Section, string Key, string Default) { RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software\\MyServer\\cdkey"); if (registryKey != null) { object objValue = registryKey.GetValue(Key, Default); if (objValue != null) { Default = Encryption.DisEncryPW(objValue.ToString(), key2); } registryKey.Close(); } return Default; }
public static string InitRegedit(out string expireDate) { string res = ""; expireDate = ""; /*检查注册表 BFEBFBFF000206A7-20190112*/ string SericalNumber = ReadSetting("", "SerialNumber", "-1"); if (SericalNumber == "-1") { res = "软件尚未注册,请注册软件!"; } /* 比较CPUid */ string CpuId =SericalNumber.Split('-')[0]; string CpuIdThis = GetCpuId(); if (CpuId != CpuIdThis) { res = "注册机器与本机不一致,请联系管理员!"; } /* 比较时间 */ string NowDate = DateTime.Now.ToString("yyyyMMdd"); string EndDate = SericalNumber.Split('-')[1]; if (Convert.ToInt32(EndDate) - Convert.ToInt32(NowDate) < 0) { res = "软件试用已到期!"; } DateTime dt = DateTime.ParseExact(EndDate, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); expireDate = dt.ToString("yyyy年MM月dd日"); return res; }
http://www.cnblogs.com/kakake/p/3938262.html
QQ:273999072 框架学习中