Results 1 to 4 of 4

Thread: Set and delete registry value

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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);
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  4. #4

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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
  •  



Click Here to Expand Forum to Full Width