Search code examples
c#windows-10regedit

Userinit autolaunch C# Windows 10


I tried this code to add my program to \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon Userinit autolaunch in C# Windows 10. This is my code:


using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TempCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AddToRegedit();
        }
        void AddToRegedit()
        {
            string programPath = @"C:\Users\XPS\Videos\TestApp.exe";
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
            string newValue = @"C:\WINDOWS\system32\userinit.exe," + programPath + ",";
            key.SetValue("Userinit", newValue, RegistryValueKind.String);
            key.Close();

            Console.WriteLine("Program added!");
        }
    }
}

I launched it with debug with admin privileges but nothing changed at all. If you guys know what's going on please help me!

I tried : Restarting my OS, Launching program without debug, and even asking ChatGPT, but nothing changed.

Also If in cmd i write REG QUERY HKLM\Software\Microsoft\Windows NT\ it says Error: Invalid syntax. Enter "REG QUERY /?" for usage help. Maybe that's because of space?


Solution

  • I fixed the problem myself here's the code:

    void RegeditValueChange()
    {
        ProcessStartInfo processInfo = new ProcessStartInfo()
        {
            FileName = "cmd",
            Arguments = "/k REG ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\" /v Userinit /t REG_SZ /d C:\\Path\\To\\Your\\File.exe /reg:64 /f"
            //Turn this 4 comments to execute cmd without window
            //UseShellExecute = false,
            //RedirectStandardOutput = true,
            //RedirectStandardError = true,
            //CreateNoWindow = true
    
        };
        using (Process process = Process.Start(processInfo))
        {
            process.WaitForExit();
            // Uncomment this for logging (if you execute without cmd window)
            /*string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();
    
    
            if (!string.IsNullOrWhiteSpace(output))
            {
                Console.WriteLine("Output: " + output);
            }
    
            if (!string.IsNullOrWhiteSpace(error))
            {
                Console.WriteLine("Error: " + error);
            }*/
        }
    
    }
    

    So if you use 32 bit system just remove /reg:64. Also I don't recomend you to have spaces in the path to your program but, if you have, try adding "" at the beggining and at the end of your path. /f makes it not to ask for replacement