2017-08-01 78 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字段進行修改。 –