zoukankan      html  css  js  c++  java
  • C# 操作注册表

         在软件开发中,为了对计算机或者相关的权限作设置,我们需要对注册表进行操作,比如最近刚刚完成了一个项目需要限制用户的使用--试用版,c/s架构的,这个是b/s的还不一样,如果是b/s的直接加个时间锁就可以了,因为获取的是服务器端的时间,用户不可能去改服务器的时间,但是c/s的就不一样了,时间都是客户端的,修改了自己的机器的时间,照样又可以继续使用,所以不可取,这个时候我们就可以对计算机的注册表进行操作,为了防止用户修改注册表,可以对写入的键值进行加密,当然了,这些都是相对的,有人会说,如果重新安装系统,不是又可以使用,呵呵,如果每次都要装系统,也划不来,这里不讨论这个,肯定会有好的方法解决,在这里说下,C#对注册表的操作:

       首先,看下注册表的结构

         

       以节点的方式展现出来,所以我们在操作的时候,要先获得节点结构,然后在对其创建还是删除,修改等

       看看实现的代码:

       首先引入库using Microsoft.Win32;

       然后打开节点,创建节点

            RegistryKey regist = Registry.LocalMachine;
            RegistryKey software = regist.OpenSubKey("software", true);

      然后就可以创建键值

                //注册信息写进注册表
                    string myID = "insertID";
                    if ((string)testCountKey.GetValue(myID, "no") == "no")//指定的键不存在
                    {
                        testCountKey.SetValue(myID, "noInstID");
                    }
                    string myKey = "insertCount";
                    if (testCountKey.GetValue(myKey, "no").ToString() == "no")//指定的键不存在
                    {
                        testCountKey.SetValue(myKey, getDiskID.Encrypt("1"));
                    }

       现在看看一个例子,试用版使用限制,到注册成正式产品的程序

       首先,先对注册表操作,创建节点和键值

     

     static class Program
        {
            /// <summary>
            
    /// 应用程序的主入口点。
            
    /// </summary>
            [STAThread]
            static void Main()
            {
                //把登录次数写入注册表
                try
                {
                    GetDiskID getDiskID = new GetDiskID();
                    RegistryKey regist = Registry.LocalMachine;
                    RegistryKey software = regist.OpenSubKey("software"true);
                    RegistryKey testCountKey = software.CreateSubKey("aaaaaa");
                    //注册信息写进注册表
                    string myID = "insertID";
                    if ((string)testCountKey.GetValue(myID, "no") == "no")//指定的键不存在 
                    {
                        testCountKey.SetValue(myID, "noInstID");
                    }
                    string myKey = "insertCount";
                    if (testCountKey.GetValue(myKey, "no").ToString() == "no")//指定的键不存在 
                    {
                        testCountKey.SetValue(myKey, getDiskID.Encrypt("1"));
                    }
                    else
                    {
                        
                        string diskID = getDiskID.GetHardDiskID();
                        string list = getDiskID.RegisteNum(diskID);
                        string  insID = testCountKey.GetValue("insertID").ToString();
                      //  string count = getDiskID.Encrypt("100");
                        
    //如果已经注册,则不需要添加
                        if (list!=insID)
                        {
                        int testCount = Convert.ToInt32(getDiskID.Decrypt(testCountKey.GetValue(myKey).ToString()));
                        testCount += 1;
                        testCountKey.SetValue(myKey, getDiskID.Encrypt(testCount.ToString()));
                        }
                    }

                
                    regist.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("注册表项设置失败!");
                    return;
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                MainForm f1 = new MainForm();
                Application.Run(f1);
            }
        }
    }

    然后,打开窗体的时候判断是否为正式版用户,这里采用了使用次数来限制,比如只能使用100次或者150次等,如果为正式版则可以正常使用,否则如果在试用期内,就继续修改注册表的次数(为了防止用户修改,可以对其加密)

    View Code
     private void MainForm_Load(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
                string diskID = getDiskID.GetHardDiskID();
       
                try
                {
                    RegistryKey regist = Registry.LocalMachine;
                    RegistryKey software = regist.OpenSubKey("software"true);
                    RegistryKey testCountKey = software.CreateSubKey("aaaaaa");
                    insID = testCountKey.GetValue("insertID").ToString();
                    string list = getDiskID.RegisteNum(diskID);
                    DataSet ds = null;
                    if (insID == "noInstID" || insID!=list)
                    {
                        this.Text += "--试用版";
                        int testCount = Convert.ToInt32(getDiskID.Decrypt(testCountKey.GetValue("insertCount").ToString()));
                        //读取限制的次数
                        ds = XMLHelper.GetDs();
                        string Tcount = getDiskID.Decrypt(ds.Tables[0].Rows[0]["count"].ToString().Trim());
                        int ttCount = Convert.ToInt32(Tcount);
                        if (testCount <= ttCount)
                        {
                            int count = ttCount - testCount;
                            MessageBox.Show("您当前使用的是试用版产品,试用期还剩下" + count.ToString() + "次。");
                            newus = new Login();
                            newus.formLogin = this;
                            this.Hide();
                            newus.Show();
                            return;
                        }
                        if (testCount > ttCount)
                        {
                            MessageBox.Show("您的试用期已到,请购买注册为正式版产品!");
                            this.Hide();
                            registerForm rf = new registerForm();
                            rf.zcID = insID;
                            rf.ShowDialog();
                            return;
                        }

                    }
                    else
                    {
                        newus = new Login();
                        newus.formLogin = this;
                        this.Hide();
                        newus.Show();
                    } 
                }
                catch
                {
                    MessageBox.Show("获取注册表信息失败!");
                    return;
                }            
              
              
            }

    最后是注册成正式版的,这里注册码采用的是 对机器的cpu序列号进行加密生成的号码,获取cpu序列号的方法

    View Code
       /// <summary>
            
    /// 取CPU序列号
            
    /// </summary>
            
    /// <returns></returns>
            public string GetHardDiskID()
            {
                string cpuInfo = "";
                ManagementClass cimobject = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }

                return cpuInfo;
            }

    注册成正式版面板代码

    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Card.UI.ClassInfo;
    using Microsoft.Win32;

    namespace Card.UI
    {
        public partial class registerForm : Form
        {
            public registerForm()
            {
                InitializeComponent();
            }
            private string _zcID;
            public string zcID
            {
                get { return _zcID; }
                set { _zcID = value; }
            }
            MD5 md5 = new MD5();
           
            string zcid = "";
            string list = "";
            GetDiskID getDiskID = new GetDiskID();
            private void registerForm_Load(object sender, EventArgs e)
            {
                try
                {
                    string CPUID = getDiskID.GetHardDiskID();
                    txtProNum.Text = CPUID;
                    list = getDiskID.RegisteNum(CPUID);
                    if (_zcID == list)
                    {
                        txtProNum.Text = _zcID;
                        txtProNum.ReadOnly = true;
                        button1.Enabled = false;
                        textBox2.Text = "";
                    }
                }
                catch
                {
                    MessageBox.Show("读取序列号失败!");
                    return;
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (txtProNum.Text != "")
                {
                    if (_zcID == "noInstID" || _zcID!=list)
                    {
                        if (txtNum.Text.Trim() == list)
                        {
                            try
                            {
                                RegistryKey regist = Registry.LocalMachine;
                                RegistryKey software = regist.OpenSubKey("software"true);
                                RegistryKey testCountKey = software.CreateSubKey("aaaaaa");
                                string myID = "insertID";
                                testCountKey.SetValue(myID, list);

                                MessageBox.Show("恭喜您注册成功!您的注册信息将于重新登录后生效");
                                                      
                                txtNum.ReadOnly = true;
                                textBox2.Text = "    ";
                                button1.Enabled = false;
                                software.Close();
                                regist.Close();
                            }
                            catch
                            {
                                MessageBox.Show("注册表读取失败!");
                                return;
                            }
                        }
                        else
                        {
                            MessageBox.Show("请填写正确的注册码!");
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("读取序列号失败!");
                    return;
                }
            }
        }
    }
  • 相关阅读:
    绿色通用的网站后台系统管理模板
    本站源码免费下载-木庄网络博客
    expr 数字操作
    wget 实现web监控脚本
    wget 监控web服务器
    read + 计算
    判断字符串长度
    替换字符串
    shell 字符串操作
    shell 定义变量
  • 原文地址:https://www.cnblogs.com/shuang121/p/2765933.html
Copyright © 2011-2022 走看看