【需要引入命名空间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);
}
}
}
}
在程序中怎样修改注册表?
最新推荐文章于 2020-01-28 13:52:25 发布