![](/img/trans.png)
[英]How can I programmatically control the Microphone and Microphone Boost settings in Win7 from C#?
[英]How can i modify registry via c# on win7?
我通過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;
}
}
}
然后像這樣調用它:
//獲取當前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();
通過這一切,我想修改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")
}
我不知道你的刪除方法是否有效,所以我也會在那里輸入我的輸入:
public void RemoveKey(string FolderName) {
Registry.CurrentUser.DeleteSubKeyTree(FolderName);
}
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.