How can I change the registry via C # to win7?

I am modifying my WIN7 computer registry with C # but it doesn't work. enter image description here my code like below:

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

      

}

and then call it like this:

//获取当前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();

      

with all of this, I want to change ProxyEnable and remove ProxyOverride, ProxyServer that overridden the IE proxy setting.

I have tried several methods, but no one can reverse the IE proxy setting. Could you help me? Thank!

+3


source to share


1 answer


Here is an example of how I will apply the IO registry as you tried to do. Depending on which part of the registry you are trying to read / write may use different keys:

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

      

The above example will read / write at the current user level, but there are other levels that can be used and you will see them as available options in IntelliSense

You can use this in your application by assigning an instance of your registry class to an object and simply calling registry.read/write, etc.

This can be checked for zeros using:

    if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\MyApp", "SomeValue", null) == null) 

      

And when you come to write data, you can use:



    myregobject.Write(@"\software\MyApp", "SomeValue", "hello world!");

      

In your case, this allows you to do the following:

    if (!Registry.GetValue(@"\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", null) == null) {
       myregobject.Write(@"\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", "your data here")
    }

      

I can't tell if your delete method is working or not by looking at it, so I'll add my input there as well:

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

      

Hope this helps!

0


source







All Articles