I know there are a lot of similar topics on SO about this and I have tried most of the solutions without success. I want to create a string value for the startup of application on Windows start in current user registry. So I have created a class for that purpose. While looking at output provided by code
Debug.WriteLine(subKey + " -- " + applicationPath);
foreach (string s in names)
{
Debug.WriteLine(" ->>> " + s);
}
I can see my application has been added. However while trying to Refresh or open close or restart my computer, I do not see this string value in my Registry path. Why so and how to fix it? Does this code work for others?
My application is 64 bit. I have tried also to change code to Local Machine and with admin rights string value is created properly in Registry path for Local Machine. However I do not want to run my application with admin rights all the time this parameter needs to be changed from application settings. For that reason I would like to use Current user. Also I have tried to do it with SID GetCurrentUserSid()
that got SID correctly, but was not able to create string value in proper path either.
Here is my current class:
public class WindowsStartupManager : IWindowsStartupManager
{
private const string RegistryKeyPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
public bool IsStartupEnabled()
{
string applicationName = GetApplicationName();
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath))
{
if (key != null)
{
return key.GetValue(applicationName) != null;
}
else
{
// Handle the case where the Registry key is not found
return false;
}
}
}
public void EnableStartup()
{
string applicationName = GetApplicationName();
string applicationPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string currentUserSid = GetCurrentUserSid();
using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, this.GetRegistryView()))
{
using (RegistryKey subKey = key.OpenSubKey(RegistryKeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl))
{
subKey.SetValue(applicationName, applicationPath, RegistryValueKind.String);
string[] names = subKey.GetValueNames();
Debug.WriteLine(subKey + " -- " + applicationPath);
foreach (string s in names)
{
Debug.WriteLine(" ->>> " + s);
}
subKey.Dispose();
}
}
}
public void DisableStartup()
{
string applicationName = GetApplicationName();
string applicationPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string currentUserSid = GetCurrentUserSid();
try
{
RegistryKey myKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, this.GetRegistryView());
{
using (RegistryKey subKey = myKey.OpenSubKey(RegistryKeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl))
{
subKey.DeleteValue(applicationName, false);
string[] names = subKey.GetValueNames();
Debug.WriteLine(subKey);
foreach (string s in names)
{
Debug.WriteLine(" - " + s);
}
subKey.Close();
}
}
}
catch (Exception ex)
{
// Handle or log the exception
Console.WriteLine("Failed to disable startup: " + ex.Message);
}
}
public string GetApplicationName()
{
Assembly assembly = Assembly.GetEntryAssembly();
return assembly.GetName().Name;
}
public string GetApplicationPath()
{
Assembly assembly = Assembly.GetEntryAssembly();
return Path.GetDirectoryName(assembly.Location);
}
private RegistryView GetRegistryView()
{
// Determine the registry view based on the application's target platform
bool is64BitProcess = Environment.Is64BitProcess;
return is64BitProcess ? RegistryView.Registry64 : RegistryView.Registry32;
}
private string GetCurrentUserSid()
{
try
{
// Get the current user's Windows identity
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
if (windowsIdentity != null)
{
// Get the user's SID
return windowsIdentity.User.Value;
}
else
{
// Handle the case where the Windows identity is not available
return string.Empty; // or throw new Exception("Windows identity not available");
}
}
catch (Exception ex)
{
// Handle or log any exceptions that may occur
Console.WriteLine("Failed to retrieve current user's SID: " + ex.Message);
return string.Empty; // or throw the exception if necessary
}
}
}