无法在c#中编辑注册表?

问题描述:

我有用于设置windows10自动登录使用C#代码, 这段代码写回来,我希望它的工作。无法在c#中编辑注册表?

但是当我测试它现在它不能够在注册表中设置键值。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace engage.client.setup 
{ 

using Microsoft.Win32; 
using System; 
using System.DirectoryServices; 

public class AutoLogin 
{ 
    public static void AutoLoginUser() 
    { 
     try 
     { 
      DirectoryEntry AD = new DirectoryEntry("WinNT://" + 
           Environment.MachineName + ",computer"); 
      DirectoryEntry NewUser = AD.Children.Add("EngageUser", "user"); 
      NewUser.Invoke("SetPassword", new object[] { "engage" }); 
      NewUser.Invoke("Put", new object[] { "Description", "EngageUser" }); 
      NewUser.CommitChanges(); 
      DirectoryEntry grp; 
      grp = AD.Children.Find("Administrators", "group"); 
      if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); } 
      Console.WriteLine("Account Created Successfully..."); 
      RemoveDefaultLogin(); 
      WriteDefaultLogin("EngageUser", "engage"); 
      Console.WriteLine("please restart your system"); 
      Console.ReadLine(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("error"+ex.Message); 
      Console.ReadLine(); 

     } 
    } 
    public static void WriteDefaultLogin(string usr, string pwd) 
    { 
     Console.WriteLine("Setting autologin..."); 
     RegistryKey rekey = Registry.LocalMachine.CreateSubKey 
      ("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"); 

     Console.WriteLine("before setting:" + rekey.GetValue("AutoAdminLogon")+ rekey.GetValue("DefaultUserName") + rekey.GetValue("DefaultPassword")); 


     if (rekey == null) 
      Console.WriteLine 
       ("There has been an error while trying to write to windows registry"); 
     else 
     { 
      rekey.SetValue("mytestingkey", "worked"); 
      rekey.SetValue("AutoAdminLogon", "1"); 
      // Console.WriteLine("set:autoadminlogon:1"+"/n reg set vlaue:"+rekey.GetValue("AutoAdminLogon")); 
      rekey.SetValue("DefaultUserName", usr); 
      // Console.WriteLine("set:username"+usr+ "/n reg set vlaue:" + rekey.GetValue("DefaultUserName")); 
      rekey.SetValue("DefaultPassword", pwd); 
      // Console.WriteLine("set:password"+ pwd+"/n reg set vlaue :" + rekey.GetValue("DefaultPassword")); 
     } 
     Console.WriteLine("after setting" + rekey.GetValue("AutoAdminLogon") + rekey.GetValue("DefaultUserName") + rekey.GetValue("DefaultPassword")); 
     rekey.Flush(); 
     rekey.Close(); 
    } 

    public static void RemoveDefaultLogin() 
    { 
     RegistryKey rekey = Registry.LocalMachine.CreateSubKey 
      ("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"); 
     if (rekey == null) 
      Console.WriteLine("Registry write error"); 
     else 
     { 
      rekey.DeleteValue("DefaultUserName", false); 
      rekey.DeleteValue("DefaultPassword", false); 
      rekey.DeleteValue("AutoAdminLogon", false); 
     } 

     rekey.Close(); 
    } 

    } 
    } 

为什么即使getvalue方法显示更新后的值,我不能更改密钥,在注册表中它没有更新。

+0

你是否用提升的权限运行它? – Adwaenyth

+0

您正在访问32位或64位注册表视图吗? – Dai

+0

是以管理员身份运行,而我的系统是64位 –

除非程序以管理员身份运行,否则无法更新Registry.LocalMachine条目。你需要添加一个清单到你的程序和make it require administrator privileges to launch

也有可能你有一个32位程序试图访问一个64位regestery密钥。即使您的应用程序运行为32位,也可以使用Registry.LocalMachine(RegistryHive.LocalMachine, RegistryView.Registry64)替换Registry.LocalMachine以强制其使用64位注册表。

你也可以编译你的程序为64位而不是32位或AnyCpu来强制它为64位。

+0

我正在运行exe文件与管理员privillage –

+0

看到我的更新,第二个选项是你有一个32位程序试图访问64位的regestery。 –

+0

......谢谢, –