Trouble writing to the registry

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

Thread Status:
Not open for further replies.
Trouble writing to the registry
  1. Unread #1 - Jan 9, 2011 at 3:03 PM
  2. ScubaSteve7
    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    ScubaSteve7 Member

    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.
     
  3. Unread #2 - Jan 10, 2011 at 8:11 AM
  4. GovindAlt
    Joined:
    Jan 5, 2011
    Posts:
    31
    Referrals:
    0
    Sythe Gold:
    0

    GovindAlt Member
    Do Not Trade

    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?
     
  5. Unread #3 - Jan 11, 2011 at 12:40 PM
  6. ScubaSteve7
    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    ScubaSteve7 Member

    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.
     
  7. Unread #4 - Jan 11, 2011 at 1:32 PM
  8. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Trouble writing to the registry

    Create the value before setting it.
     
  9. Unread #5 - Jan 12, 2011 at 4:27 PM
  10. ScubaSteve7
    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    ScubaSteve7 Member

    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);
     
  11. Unread #6 - Jan 12, 2011 at 4:35 PM
  12. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Trouble writing to the registry

    Once you open the subkey, create a new value in it.
     
  13. Unread #7 - Jan 12, 2011 at 6:14 PM
  14. ScubaSteve7
    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    ScubaSteve7 Member

    Trouble writing to the registry

    Could you provide an example? :)
     
  15. Unread #8 - Jan 12, 2011 at 6:39 PM
  16. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Trouble writing to the registry

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

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

    Spell Microsoft right ;)
     
  17. Unread #9 - Jan 13, 2011 at 6:56 AM
  18. ScubaSteve7
    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    ScubaSteve7 Member

    Trouble writing to the registry

    Man all that confusion... attributed to a spelling mistake, Thanks for the help SMR it's working.
     
  19. Unread #10 - Jan 13, 2011 at 10:16 AM
  20. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Trouble writing to the registry

    Haha, mind if I lock the thread now :p
     
  21. Unread #11 - Mar 5, 2011 at 10:30 PM
  22. ScubaSteve7
    Joined:
    Dec 1, 2010
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    ScubaSteve7 Member

    Trouble writing to the registry

    Of course not
     
< VB6/.NET/C# which do i choose? | Word chain solver >

Users viewing this thread
1 guest
Thread Status:
Not open for further replies.


 
 
Adblock breaks this site