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

Threaded View

  1. #1
    Join Date
    Aug 2005
    Posts
    25

    [RESOLVED] RegistryKey method DeleteValue not working

    Hello,

    Has any tried deleting values from a registry subkey using .NET? I'm having trouble using the DeleteValue() method, because it always throws an exception whenever I try to delete a value, even though I know the value exists and I'm logged in as an Administrator on Windows XP.

    This code always gets a System.UnauthorizedAccessException, with a detail that it "Cannot write to the registry key." I've tried deleting values other than ones in "Software\Microsoft\Windows\CurrentVersion\Run\" but that didn't work either.

    Also, I know how to do it in C++, and that works, but I can't get this to work in .NET.

    Here is the code I've tried using:

    Code:
    
    //if the value name exists, delete it
     
    string subKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run\"; //subkey name
     
    string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
     
    try
     
    {
     
    RegistryKey reg = Registry.LocalMachine.OpenSubKey(subKeyName);
     
    if (reg != null)
     
    {
     
    reg.DeleteValue(valueName);
     
    reg.Close();
     
    }
     
    }
     
    catch (Exception ex)
     
    {
     
    MessageBox.Show(this, ex.ToString());
     
    }
     
    
    Also, I've tried this code snippet from http://www.c-sharpcorner.com/Tutoria...egTutCSMCB.asp, but that didn't work for me either. (I created a string value as the code says using regedit.exe.)

    Code:
     
    // Delete the key value
    RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\");
    delKey.DeleteValue("MCBInc");
    Any thoughts?

    Many thanks,
    MartyP
    Last edited by MartyP; April 24th, 2006 at 10:37 AM.

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