如何在win7上通过c#修改注册表?
我通过c#修改WIN7计算机的注册表,但它不起作用。 我的代码如下所示:如何在win7上通过c#修改注册表?,c#,internet-explorer,proxy,windows-7,registry,C#,Internet Explorer,Proxy,Windows 7,Registry,我通过c#修改WIN7计算机的注册表,但它不起作用。 我的代码如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; //添加针对操作注册表的应用 namespace operateToolWPF.Utils { class RegisterHelper { public static str
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32; //添加针对操作注册表的应用
namespace operateToolWPF.Utils
{
class RegisterHelper
{
public static string GetRegistryData(RegistryKey root, string subKey,
string name)
{
string registData = string.Empty;
RegistryKey myKey = root.OpenSubKey(subKey, true);
if (myKey != null)
{
registData = myKey.GetValue(name).ToString();
}
return registData;
}
/// <summary>
/// 向注册表中写数据
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="keyName"></param>
/// <param name="keyValue"></param>
public static void SetRegistryData(RegistryKey root, string subKey, string keyName, Int32 keyValue)
{
RegistryKey aimDir = root.CreateSubKey(subKey);
aimDir.SetValue(keyName, keyValue, RegistryValueKind.DWord);
}
/// <summary>
/// 删除注册表中指定的注册项
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="keyName"></param>
public static void DeleteRegist(RegistryKey root, string subKey, string keyName)
{
string[] subkeyNames;
RegistryKey myKey = root.OpenSubKey(subKey, true);
subkeyNames = myKey.GetSubKeyNames();
foreach (string aimKey in subkeyNames)
{
if (aimKey == keyName)
myKey.DeleteSubKeyTree(keyName);
}
}
/// <summary>
/// 判断指定注册表项是否存在
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="keyName"></param>
/// <returns></returns>
public static bool IsRegistryExits(RegistryKey root, string subKey, string keyName)
{
bool result = false;
string[] subKeyNames;
RegistryKey myKey = root.OpenSubKey(subKey, true);
subKeyNames = myKey.GetValueNames();
foreach (string name in subKeyNames)
{
if (name == keyName)
{
result = true;
return result;
}
}
return result;
}
}
通过所有这些,我想要修改ProxyEnable并删除ProxyOverride,ProxyServer,这是取消IE代理设置
我尝试了几种方法,但没有人可以取消IE代理设置。
你能帮助我吗?谢谢 下面是一个示例,说明我将如何像您尝试的那样实现注册表IO。根据您尝试读取/写入注册表的哪个部分,可能会使用不同的键:
public class MyReg{
public RegistryKey Foriegnkey {
get => forignKey;
set => forignKey = value;
}
private RegistryKey forignKey;
public object Read(string Path, string Name) => (Registry.CurrentUser.OpenSubKey(Path, false).GetValue(Name));
public void Write(string Path, string Name, object Data) {
Foriegnkey = Registry.CurrentUser.CreateSubKey(Path, RegistryKeyPermissionCheck.Default);
Foriegnkey.SetValue(Name, Data);
Foriegnkey.Close();
}
}
上面的示例将以当前用户级别进行读/写,但还有其他级别可以使用,您将在IntelliSense中看到这些级别作为可用选项
您可以在应用程序中使用它,方法是将注册表类的实例分配给对象,然后只调用registry.read/write等
可以使用以下方法检查空值:
if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\MyApp", "SomeValue", null) == null)
当您开始编写数据时,您可以使用:
myregobject.Write(@"\software\MyApp", "SomeValue", "hello world!");
在您的情况下,这使您能够执行以下操作:
if (!Registry.GetValue(@"\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", null) == null) {
myregobject.Write(@"\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", "your data here")
}
我看不出你的delete方法是否有效,所以我也会在那里输入:
public void RemoveKey(string FolderName) {
Registry.CurrentUser.DeleteSubKeyTree(FolderName);
}
希望这有帮助 谢谢你的帮助!但ot不起作用,trow异常:System.ArgumentException:注册表项名称必须以有效的基项名称开头。?你能提供更多关于你到底想做什么的信息吗?一步一步地。我可以在此基础上添加另一个代码示例,以确保代码适合您:)我开发了一个WPF应用程序,它使用代理工具Fiddler提供的FiddlerCoreAPI来捕获HTTP请求数据。最后,使用fiddlercore API提供的Doquit()方法关闭代理,虽然代理已关闭,但此时使用C#发出post请求,并且无法连接Internet。因此,现在我想使用代码强制修改注册表中的EnableProxy字段。包含键“\Software\Microsoft\Windows\CurrentVersion\Internet Settings”的根节点是什么?还有-其目的是什么:
asdf Int32 tempInt=0//预先定义一个有符号32位数 //未经检查语句块内的转换,不做溢出检查 未选中{tempInt=Convert.ToInt32(“00000000”,16)//强制转换成有符号32位数 }代码>为了强制启用代理字段需要插入什么值?根节点是HKEY_CURRENT_USER。转换为16十六进制的原因是注册表以十六进制数字存储。即使不是16十六进制,也不会导致错误。
public void RemoveKey(string FolderName) {
Registry.CurrentUser.DeleteSubKeyTree(FolderName);
}