Results 1 to 7 of 7

Thread: what is wrong with this code?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Question what is wrong with this code?

    hello!

    I have written a code that writes and reads from the registry.
    to write:
    Code:
            // Create a new key under HKEY_LOCAL_MACHINE\Software as MCBInc
           RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
            // Add one more sub key
           RegistryKey newkey = key.CreateSubKey("reg");
            // Set value of sub key
            newkey.SetValue("reg", toWrite);
    to read:

    Code:
            // Create a new key under HKEY_LOCAL_MACHINE\Software as MCBInc
            RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
            // Get sub key
            RegistryKey newkey = key.OpenSubKey("reg");
            // Get value of sub key
            return ((string)key.GetValue("reg"));
    the problem is, that I can't read what I wrote! I am able to write to registry.

    what is wrong?

    thanks!

    Dekel C.

  2. #2
    New Member
    Join Date
    Nov 2005
    Posts
    13

    Re: what is wrong with this code?

    // Create a new key under HKEY_LOCAL_MACHINE\Software as MCBInc
    RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
    // Get sub key
    RegistryKey newkey = key.OpenSubKey("reg");
    // Get value of sub key
    return ((string)key.GetValue("reg"));



    I think you need (string) newkey.GetValue("reg") in that last line..
    I tried and it worked

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: what is wrong with this code?

    Here a good article showing how to do it..
    http://www.csharphelp.com/archives2/archive430.html

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: what is wrong with this code?

    I have anothe question...

    How can I know if the registry key even exists?

    thanks
    Dekel C.

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

    Re: what is wrong with this code?

    The OpenSubKey method returns null if the specified key does not exist. GetValue does the same. The RegistryKey class also has GetSubKeyNames and GetValueNames methods you can use to get a String array of all subkey and value names. Let me also add that .NET apps should generally avoid using the registry to store their own data if possible.
    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

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: what is wrong with this code?

    why should they avoid using the registry?
    Dekel C.

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

    Re: what is wrong with this code?

    The registry is basically out of control. It now contains so much data, and much of it useless, that it can be a reason that computers run at less than optimum efficiency. To reduce the dependency of applications on the registry Microsoft recommends using XML as much as is practically possible for each application to store there own data locally. The application config file and serialisation are two of the tools available to you to implement this. I'm not sure exactly how things have changed in C# 2005 but VB 2005 has made the process of persisting application ridiculously simple through the My.Settings object. I'd assume that improvements have been made to config file handling in C# 2005 has improved as well, although there are many examples on the Net of how to replicate the My namespace in C# because it is such a great tool.
    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

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