gpt4 book ai didi

c# - 我如何在win7上通过c#修改注册表?

转载 作者:行者123 更新时间:2023-11-30 18:16:54 26 4
gpt4 key购买 nike

我用c#修改了我的WIN7电脑的注册表,但是没有用。 enter image description here我的代码如下:

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。根据您尝试读取/写入的注册表部分,可能会使用不同的 key :

    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);
}

希望这对您有所帮助!

关于c# - 我如何在win7上通过c#修改注册表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45434318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com