Set and delete registry value
hi guys i have a code below to set a registry value, though im not sure if it is the right way to make my application run automatically when windows start, but i dont have yet a code that will delete the that value i've set. i've tried the Registry.LocalMachine.DeleteValue but it didn't work.
Code:
if (chkEnableOnStartup.Checked)
{
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", Application.ProductName.ToString(), Application.ExecutablePath.ToString());
}
else
{
//Code to delete value here
}
Re: Set and delete registry value
Given that ProductName and ExecutablePath both return String objects, your calls to ToString are somewhat redundant. The Registry class has GetValue and SetValue methods but no DeleteValue method. You need to use the RegistryKey.DeleteValue method. The RegistryKey class also has GetValue and SetValue methods:
Code:
public void SetRunAtStartup(bool run)
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (run)
{
runKey.SetValue(Application.ProductName, Application.ExecutablePath);
}
else if (runKey.GetValue(Application.ProductName) != null)
{
runKey.DeleteValue(Application.ProductName);
}
}
Re: Set and delete registry value
This is how you set a value and delete it
Code:
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true).SetValue(Application.ProductName.ToString(), Application.ExecutablePath.ToString());
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true).DeleteValue(Application.ProductName.ToString());
Re: Set and delete registry value
Thanks for the Info. Jm..and Thanks for the inputs guys!