i have code which is used for setting windows10 auto login using c#, this code was written way back, i hope it worked then.
but when i tested it now it is not able to set the key value in registry.
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();
}
}
}
why i am not able to change the key even if getvalue method show the updated value, in registry it is not updating.