Code works in one case, but not in a similar case (registry editing). Help?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Code works in one case, but not in a similar case (registry editing). Help?

  1. #1
    Join Date
    May 2007
    Posts
    8

    Code works in one case, but not in a similar case (registry editing). Help?

    I'm trying to simply copy a registry key using the pre-built code here: http://www.codeproject.com/Articles/...y-Keys#_rating (note: the actual code is from the forum reply "Re: problem" by fallafab".

    edit: the code is visible in the pictures below. I didn't realize this forum doesn't display IMG tags.

    When I say that I want to copy ".nip" to ".nip_bak", the destination key is set properly:



    However, when I want to copy "nip_auto_file" to "nip_auto_file_bak", the destination key is set to null:



    Both registry entries already exist. What can possibly be causing this problem?

    edit: I'm using .NET 4.0
    Last edited by Danja; August 8th, 2012 at 12:58 PM.

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    Re: Code works in one case, but not in a similar case (registry editing). Help?

    According to the MSDN entry for RegistryKey.OpenSubKey Method (String, Boolean), this method will return null if the registry key does not exist, otherwise it will either open the key or throw one of the following exceptions:

    ArgumentNullException - The key name is null
    ObjectDisposedException - The RegistryKey is closed (closed keys cannot be accessed).
    SecurityException - The user does not have the permissions required to access the registry key in the specified mode.

    If you had also posted pictures of regedit showing the keys involved, we could have verified them here.

    Since you did not say anything about getting an exception, the only conclusion left is that the key does not exist. Verify that the full key name is correct and that the key exists in the registry. Be extra careful about making sure that the spelling (all elements of the key name) matches perfectly.

  3. #3
    Join Date
    May 2007
    Posts
    8

    Re: Code works in one case, but not in a similar case (registry editing). Help?

    Thank you for your reply. I'm fairly certain that the key exists because the function above is actually called the the following lines:

    Code:
    if (Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Classes").OpenSubKey("nip_auto_file") != null)
                {
                    RegManagement.RenameSubKey(Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Classes"), "nip_auto_file", "nip_auto_file_bak");
    
    //RegManagement.RenameSubKey simply calls RegManagement.CopyKey, and then deletes the parent key
                }
    It should be noted that if, in the CopyKey function, I change "using (RegistryKey destinationKey = parentKey.OpenSubKey(newKeyName, true))" to "using (RegistryKey destinationKey = parentKey.CreateSubKey(newKeyName))", I get an UnauthorizedAccessException even though I'm running Visual Studio as administrator. Could that point to the problem?

  4. #4
    Join Date
    Jul 2012
    Posts
    90

    Re: Code works in one case, but not in a similar case (registry editing). Help?

    From what I'm seeing here, it may be related to the fact that you are opening the parent key (Software) that you are passing into CopyKey as read. You may need to open it as read/write using RegistryKey.OpenSubKey(string, bool) before passing it into CopyKey. Opening (nip_auto_file) as read for the existance check should be fine.

  5. #5
    Join Date
    May 2007
    Posts
    8

    Re: Code works in one case, but not in a similar case (registry editing). Help?

    Quote Originally Posted by CGKevin View Post
    From what I'm seeing here, it may be related to the fact that you are opening the parent key (Software) that you are passing into CopyKey as read. You may need to open it as read/write using RegistryKey.OpenSubKey(string, bool) before passing it into CopyKey. Opening (nip_auto_file) as read for the existance check should be fine.
    Thank you! That was the problem. I opened the Software subkey as write and it worked. Thank you very much for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center