-3

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.

  • Did you run it with elevated privileges? – Adwaenyth Aug 04 '17 at 05:32
  • Are you accessing the 32-bit or 64-bit registry view? – Dai Aug 04 '17 at 05:33
  • yes ran as admin, and my system is 64bit – Vishnu prasad.k.v Aug 04 '17 at 05:36
  • 1
    "not able to" is an insufficient problem description. What happens instead? Do you get an error message? I see you are catching exceptions. Does one occur? Please add more information to your question to give context. The fact that you were running with admin privileges, and on a 64-bit system, should have been in the question to start with. That would have prevented Scott Chamberlain from having to guess and post an unhelpful answer. – Cody Gray - on strike Aug 04 '17 at 05:38

1 Answers1

0

You can't update Registry.LocalMachine entries unless your program is running as an administrator. You need to add a manifest to your program and make it require administrator privileges to launch.

It is also is possible that you have a 32 bit program trying to access a 64 bit regestery key. Replace Registry.LocalMachine with Registry.LocalMachine(RegistryHive.LocalMachine, RegistryView.Registry64) to force it to use the 64 bit registry even if your app is running as 32 bit.

You also could compile your program as 64 bit instead of 32 bit or AnyCpu to force it to be 64 bit.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431