C
C#9mo ago
Alex

Adding program to Local Machine Registry

Hey there! I've been trying to add a program to the local machine registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run so that it runs on boot, but this seemingly isn't working. I have administrator privileges.
// Get the key to be added to the Windows Registry
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
string bootScriptFilepath = Path.Combine(serverSection.AddSection(ScriptsSectionName).SectionFullPath, BootScriptName);

if (serverSection.GetFirstDocumentNamed("boot.bat") == null)
{
// Create the .bat file to be added to the Windows Registry
string[] command = BuildStringForStartupRegistryCommand(serverSection.SimpleName).Split('\n');
FileUtils.DumpToFile(bootScriptFilepath, command.ToList().Select(s => s.Trim()).ToList());
}

// Add the key to the Windows Registry if it doesn't already exist
if (key != null && !IsServerInRegistry(serverSection))
{
key.SetValue("Glowberry-" + serverSection.SimpleName, bootScriptFilepath);
key.Flush();
key.Close();
return true;
}

return false;
// Get the key to be added to the Windows Registry
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
string bootScriptFilepath = Path.Combine(serverSection.AddSection(ScriptsSectionName).SectionFullPath, BootScriptName);

if (serverSection.GetFirstDocumentNamed("boot.bat") == null)
{
// Create the .bat file to be added to the Windows Registry
string[] command = BuildStringForStartupRegistryCommand(serverSection.SimpleName).Split('\n');
FileUtils.DumpToFile(bootScriptFilepath, command.ToList().Select(s => s.Trim()).ToList());
}

// Add the key to the Windows Registry if it doesn't already exist
if (key != null && !IsServerInRegistry(serverSection))
{
key.SetValue("Glowberry-" + serverSection.SimpleName, bootScriptFilepath);
key.Flush();
key.Close();
return true;
}

return false;
That's the code I'm using; There's more that checks if it is in and stuff, and whilst key.GetValue() returns an actual value and not null, the entry isn't added to regedit, and my program doesn't actually start on boot. Help?
1 Reply
Alex
AlexOP9mo ago
Nevermind, moved on to using the TaskSchedulerLibrary
Want results from more Discord servers?
Add your server