Adblock breaks this site

Trouble writing to the registry

Discussion in 'Programming General' started by ScubaSteve7, Jan 9, 2011.

Thread Status:
Not open for further replies.
  1. ScubaSteve7

    ScubaSteve7 Member

    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    Trouble writing to the registry

    So this is my attempt at writing to the registry for my program to open up when windows starts.

    Code:
    RegistryKey App = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsft\\Windows\\CurrentVersion\\Run", true);
    
                    if (App.GetValue("MyApp") == null)
                    {
                        App.DeleteValue("MyApp", false);
                        App.SetValue("MyApp", System.Reflection.Assembly.GetExecutingAssembly().Location.ToString());
                        Console.WriteLine("Program runs at startup.");
                    }
                    else
                        Console.WriteLine("Program runs at startup.");
    When trying to write the executable path to the registry, I get his error:
    Code:
    System.NullReferenceException: Object reference not set to an instance of an object.
       at App.Program.Main(String[] args) in C:\Users\Steve\Desktop\csLK\csLK\
    Program.cs:line 41
    Any ideas as to what I'm doing wrong? I'm a newb.
     
  2. GovindAlt

    GovindAlt Member
    Do Not Trade

    Joined:
    Jan 5, 2011
    Posts:
    31
    Referrals:
    0
    Sythe Gold:
    0
    Trouble writing to the registry

    Don't know .NET too well, but here's my guess:
    App.DeleteValue("MyApp", false);

    Why are you deleting an object which you've already determined doesn't exist? Shouldn't it be CreateValue first?
     
  3. ScubaSteve7

    ScubaSteve7 Member

    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    Trouble writing to the registry

    Ahh yea I do already check for that, I had thrown in the delete in some attempt to try to make it work lol.
    Still having trouble.
     
  4. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Trouble writing to the registry

    Create the value before setting it.
     
  5. ScubaSteve7

    ScubaSteve7 Member

    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    Trouble writing to the registry

    I thought that's what i was doing by doing:
    RegistryKey App = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsft\\Windows\\CurrentVersion\\Run", true);
     
  6. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Trouble writing to the registry

    Once you open the subkey, create a new value in it.
     
  7. ScubaSteve7

    ScubaSteve7 Member

    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    Trouble writing to the registry

    Could you provide an example? :)
     
  8. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Trouble writing to the registry

    Oh wait, my bad, there's no CreateValue thing in .NET.

    SOFTWARE\\Microsft\\Windows\\CurrentVersion\\Run

    Spell Microsoft right ;)
     
  9. ScubaSteve7

    ScubaSteve7 Member

    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    Trouble writing to the registry

    Man all that confusion... attributed to a spelling mistake, Thanks for the help SMR it's working.
     
  10. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Trouble writing to the registry

    Haha, mind if I lock the thread now :p
     
  11. ScubaSteve7

    ScubaSteve7 Member

    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    Trouble writing to the registry

    Of course not
     
< VB6/.NET/C# which do i choose? | Word chain solver >
Thread Status:
Not open for further replies.


 
 
Adblock breaks this site