2017-08-01 73 views
3

我通过c#修改了我的WIN7计算机的注册表,但它并不奏效。 enter image description here 我的代码如下喜欢:我怎样才能修改注册表通过c#在win7上?

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,访问代理服务器,这是

//获取当前Windows用户 
     WindowsIdentity curUser = WindowsIdentity.GetCurrent(); 
     //用户SID 
     SecurityIdentifier sid = curUser.User; 
     //用户全称 
     NTAccount ntacc = (NTAccount)sid.Translate(typeof(NTAccount)); 
     Console.WriteLine("Windows SID:" + sid.Value); 
     Console.WriteLine("用户全称:" + ntacc.Value); 
     Int32 tempInt = 0; //预先定义一个有符号32位数 
     //unchecked语句块内的转换,不做溢出检查 
     unchecked 
     { 
      tempInt = Convert.ToInt32("00000000", 16); //强制转换成有符号32位数 
     } 
     //读取Display Inline Images 
     string displayImgPath = sid.Value + @"\Software\Microsoft\Windows\CurrentVersion\Internet Settings"; 
     string ProxyEnable = RegisterHelper.GetRegistryData(Registry.Users, displayImgPath, "ProxyEnable"); 
     //此时的tempInt已经是有符号32位数,可以直接写入注册表 
     RegisterHelper.SetRegistryData(Registry.Users, displayImgPath, "ProxyEnable", tempInt); 
     Thread.Sleep(3000); 
     RegisterHelper.DeleteRegist(Registry.Users, displayImgPath, "ProxyServer"); 
     RegisterHelper.DeleteRegist(Registry.Users, displayImgPath, "ProxyOverride"); 
     Registry.Users.Close(); 
     Process[] MyProcess = Process.GetProcessesByName("explorer"); 
     MyProcess[0].Kill(); 

取消IE代理设置。

我已经尝试了几种方法,但没有人可以取消IE代理设置。 你能帮我吗?谢谢!

回答

0

以下是我将如何实现注册表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(); 
    } 
    } 

上面的例子将在当前用户级读/写,但也有可以使用其他级别,并你会看到智能感知中的这些可用选项

可以通过设置您的注册表类的实例对象,只是调用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") 
    } 

我不能告诉我们,如果你删除方法的工作或不是看着它,所以我会在那里投入我的输入:

public void RemoveKey(string FolderName) { 
     Registry.CurrentUser.DeleteSubKeyTree(FolderName); 
    } 

希望这有助于!

+0

感谢您的帮助!但是没有工作,并且抛出异常:System.ArgumentException:注册表项名称必须以有效的基项名称开头。? –

+0

你能提供更多关于你想要做什么的信息吗?一步步。我可以在此基础上添加另一个代码示例以确保代码适用于您:) – jasttim

+0

我开发了一个WPF应用程序,该应用程序使用代理工具Fiddler提供的fiddlercore API来捕获HTTP请求数据。最后,使用由fiddlercore API提供的Doquit()方法,虽然代理已关闭,但代理已关闭,但C#用于在此时发出发布请求,并且Internet无法连接。 因此,现在我想要使用代码强制注册表中的EnableProxy字段进行修改。 –