繁体   English   中英

如何在win7上通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM