How can i modify registry via c# on win7?

Viewed 102

I modify my WIN7 computer's registry via c#,but it dosen't work. enter image description here my code likes 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();

by all this ,i want modify ProxyEnable and delete ProxyOverride,ProxyServer,which was cancel IE proxy setting.

I have tried several methodes,but have no one can cancel IE proxy setting. Can you help me? Thanks!

1 Answers
Related