CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Registry Keys

  1. #1
    Join Date
    Jun 2008
    Posts
    52

    Registry Keys

    Well I'm trying to create a registry reader and this is what I have so far:

    Code:
                RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Wow6432Nod\\VMware, Inc.\\VMware Workstation\\License.ws.6.0.200907");
                
                if (regkey.GetValue("CompanyName") != null)
                {
                    string showIt = regkey.GetValue("CompanyName").ToString();
                    MessageBox.Show(showIt);
                }
    that however doesn't do anything, I'm just trying to read the company name form VMware Workstation, can anyone explain to me why my code doesn't work and how can I fix it? Thanks

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Registry Keys

    I think it should be

    Code:
    RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Wow6432Node\\VMware, Inc.\\VMware Workstation\\License.ws.6.0.200907");
    Node the 'Wow6432Nod' becomes 'Wow6432Node'.

    Check the path of the registry string : if it doesn't exist you won't get any results !

    Also you should be doing this to ensure the key is closed when you've finished with it :

    Code:
    using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Wow6432Node\\VMware, Inc.\\VMware Workstation\\License.ws.6.0.200907"))
    {
        if (regkey.GetValue("CompanyName") != null)
        {
            string showIt = regkey.GetValue("CompanyName").ToString();
            MessageBox.Show(showIt);
        }
    }

    Darwen.
    Last edited by darwen; August 10th, 2008 at 04:51 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jun 2008
    Posts
    52

    Re: Registry Keys

    Quote Originally Posted by darwen
    I think it should be

    Code:
    RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Wow6432Node\\VMware, Inc.\\VMware Workstation\\License.ws.6.0.200907");
    Node the 'Wow6432Nod' becomes 'Wow6432Node'.

    Check the path of the registry string : if it doesn't exist you won't get any results !

    Also you should be doing this to ensure the key is closed when you've finished with it :

    Code:
    using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Wow6432Node\\VMware, Inc.\\VMware Workstation\\License.ws.6.0.200907"))
    {
        if (regkey.GetValue("CompanyName") != null)
        {
            string showIt = regkey.GetValue("CompanyName").ToString();
            MessageBox.Show(showIt);
        }
    }

    Darwen.

    Still doesn't work, the message box never shows

    EDIT: ohh one more thing how would I read the defult value in the folder cause on this one im trying to read the "CompanyName" value
    Last edited by Korupt; August 10th, 2008 at 12:17 PM.

  4. #4
    Join Date
    Jul 2005
    Posts
    7

    Re: Registry Keys

    http://www.codeproject.com/KB/cs/readwritexmlini.aspx

    Try that, no need trying to reinvent the wheel.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Registry Keys

    Quote Originally Posted by Korupt
    Still doesn't work, the message box never shows

    EDIT: ohh one more thing how would I read the defult value in the folder cause on this one im trying to read the "CompanyName" value
    Are yo stepping into the code in a debugger? If you don't know how to do this, please say so so we can explain how.

    In answer to how to get the default value... Just pass in a "" to the GetValue() method.

  6. #6
    Join Date
    Jun 2008
    Posts
    52

    Re: Registry Keys

    it's cool i figured it out and thanks for the input

  7. #7
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    Re: Registry Keys

    Also you should be doing this to ensure the key is closed when you've finished with it :

    Code:
    using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Wow6432Node\\VMware, Inc.\\VMware Workstation\\License.ws.6.0.200907"))
    {
        if (regkey.GetValue("CompanyName") != null)
        {
            string showIt = regkey.GetValue("CompanyName").ToString();
            MessageBox.Show(showIt);
        }
    }
    Darwen, I developed a class to easily access the Registry, so I store the RegistryKey regkey as a member variable.
    I am wondering:
    How can I ensure the key is closed when I've finished with it, since I OpenSubKey in my constructor, then I GetValue in other functions (I cannot enclose it in a using case)?
    Last edited by MontgomeryBurns; October 12th, 2010 at 05:40 AM.
    Mr. Burns

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Registry Keys

    Quote Originally Posted by MontgomeryBurns View Post
    Darwen, I developed a class to easily access the Registry, so I store the RegistryKey regkey as a member variable.
    I am wondering:
    How can I ensure the key is closed when I've finished with it, since I OpenSubKey in my constructor, then I GetValue in other functions (I cannot enclose it in a using case)?
    Are you aware that ATL already has a registry class? It's called CRegKey. Just include <atlbase.h> to use it.

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

Featured