|
-
Oct 26th, 2006, 05:43 AM
#1
Thread Starter
Fanatic Member
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
}
Last edited by daimous; Oct 26th, 2006 at 05:47 AM.
-
Oct 26th, 2006, 08:47 AM
#2
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);
}
}
-
Oct 26th, 2006, 08:50 AM
#3
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());
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 26th, 2006, 06:33 PM
#4
Thread Starter
Fanatic Member
Re: Set and delete registry value
Thanks for the Info. Jm..and Thanks for the inputs guys!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|