在程序中怎样修改注册表?

 
【需要引入命名空间Microsoft.Win32;】
1.创建注册表
	 RegistryKey key = Registry.LocalMachine;
         RegistryKey software = key.CreateSubKey("software\\test");
2.打开注册表
	 RegistryKey key = Registry.LocalMachine;
         RegistryKey software = key.OpenSubKey("soft\\test", true);
3.删除注册表
	 RegistryKey key = Registry.LocalMachine;
         key.DeleteSubKey("software\\test", true);
         key.Close();
4.为注册表赋值
	 RegistryKey key = Registry.LocalMachine;
         RegistryKey software = key.OpenSubKey("software\\test",true);
         software.SetValue("test", "coolszy");  
5.判断注册表是否存在
	 RegistryKey key = Registry.LocalMachine;
         RegistryKey software = key.OpenSubKey("software");
         string[] keys =software.GetSubKeyNames();
         foreach (string s in keys) 
         {
             if (s == "test") 
             {
                   MessageBox.Show("存在");
             }
         }
注意:关闭注册表	key.close()

【案例】      
	 private void Form3_Load(object sender, EventArgs e)
         {
            RegistryKey key = Registry.LocalMachine;
            RegistryKey count = key.CreateSubKey("software\\count");
            count.SetValue("count", 1);
         }

	   private void button1_Click(object sender, EventArgs e)
          {
            if (number < 10)
            {
                RegistryKey key = Registry.LocalMachine;
                RegistryKey count = key.OpenSubKey("software\\count",true);
                number = Convert.ToInt32(count.GetValue("count"));
                number++;
                count.SetValue("count", number);          
            }
            else {
                button1.Enabled = false;
            }
           }       



1.禁止修改IE主页
	注册表位置:HKEY_CURRENT_USER\SoftWare\Policies\Microsoft\Internet Explorer\Control Panel\HomePage
	值:0=>可以修改设置主页、1=>不能修改主页
2.设置IE主页
	注册表位置:HKEY_LOCAL_MACHINE\SoftWare\Microsoft\Internet Explorer\Main\Start Page
	值:想要设置的网址
3.修改IE浏览器标题栏的内容
	注册表位置:HKEY_LOCAL_MACHINE\SoftWare\Microsoft\Internet Explorer\Main\Window Title
	值:想要设置的标题
4.隐藏IE浏览器的右键菜单
	注册表位置:HKEY_CURRENT_USER\SoftWare\Policies\Microsoft\Internet Explorer\Restrictions\NoBrowserContextMenu
	值:1=>右键菜单不会显示,删除子项就会被显示
5.禁止使用注册表
	注册表位置:HKEY_CURRENT_USER\SoftWare\Microsoft\Windows\CurrentVersion\Policies\System\DisableRegistryTools
	值:1=>禁止使用注册表,删除子项就能使用
6.隐藏、显示桌面
	注册表位置:HKEY_CURRENT_USER\SoftWare\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDeskTop
	值:1=>隐藏桌面	2=>显示桌面
7.程序启动的次数
	代码如下:
	    【窗体加载事件】
	    RegistryKey key = Registry.LocalMachine;
            RegistryKey registry = key.OpenSubKey("software\\test");
            if (registry == null)
            {
                RegistryKey reg = key.CreateSubKey("software\\test");
                reg.SetValue("num", 1, RegistryValueKind.String);
            }
	    【按钮事件】
		int num = 0;
	     if (num < 10)
            {
                RegistryKey key = Registry.LocalMachine;
                RegistryKey reg = key.OpenSubKey("software\\test", true);
                num = Convert.ToInt32(reg.GetValue("num"));
                num++;
                reg.SetValue("num", num,RegistryValueKind.String);
            }
            else {
                button1.Enabled = false;
            }
	  

8.设置开机启动项
	private void WriteRegistry()
        {
            string strName = Application.ExecutablePath;
            if (File.Exists(strName))
            {
                string strNewName = Path.GetFileName(strName);
                RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (reg == null)
                {
                    reg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                }
                else
                {
                    if (reg.GetValue(strNewName)==null)
                    {
                        reg.SetValue(strNewName, strName);
                    }
                }
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值