Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi guys,

I'm trying to delete a registry key in windows mobile

I've tryed looking the code on google
(c# delete registry key value)[^]
A lot of info is provided, but it doen't get me to solve it.

I tried
http://www.codeproject.com/KB/system/modifyregistry.aspx
But didn't get it to work


I got it to write a key, check if a key is there
But I get exceptions when deleting.

I'm using visual Studio 2008 pro writing a app. for smart device in C#

I'm am new to programming but got an app up and running by studing home and learn website , copy and editing code provided at internet (msdn, codeproject, google etc)

I want do use the registry, so I created a test app. to test write/read and delete Registry keys/values.

Is there someone who can provide a working code?

I can provide the source file (project) if handy?

Thankz in advance

Code underneath:
Button1: write key
Button2: checks key is prsent
Button3 should delete key.

I think I need an overloador something
(I dont know what an overload is, Im not a programmer,
trying to get somewhere, but watching, using, modifying and thus learning)


using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace RegistryKeys_TEST
{
    public partial class Frm_REG_TEST : Form
    {
        public Frm_REG_TEST()
        {
            InitializeComponent();
        }

        private void btn_write_key_Click(object sender, EventArgs e)
        {
            //using Microsoft.Win32;
            // Write key "test" with subkey "DeleteMe"
            RegistryKey versie1 = Registry.LocalMachine.CreateSubKey("SOFTWARE\\test\\DeleteMe");
            // versie1.SetValue("Version", "1");
            versie1.Close();

        }

        private void btn_rk_exist_Click(object sender, EventArgs e)
        {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\test\\DeleteMe");

            if (rk != null)
            {
                MessageBox.Show("YES");// It's there
            }
            else
            {
                MessageBox.Show("NO");// It's not there
            }

        }

        private void REG_TEST_L_exit_Click(object sender, EventArgs e)
        {
            // exit app.
            Application.Exit();
        }

        private void REG_TEST_R_form2_Click(object sender, EventArgs e)
        {
            // Ga naar Form 2, 2eform kan alles zijn.
            Form2 nogeenform = new Form2();
            nogeenform.Show();

        }

        private void btn_del_key_Click(object sender, EventArgs e)
        {
            // Open SOFTWARE
            RegistryKey versie1 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\test");
            // Delete "DeleteMe" from SOFTWARE
            versie1.DeleteSubKeyTree("DeleteMe");
            //Close the registry SubKey and flush
            versie1.Close();
        }


        }
    }







Regards,

Frans
Posted
Updated 14-Mar-11 12:47pm
v2

DeleteSubKeyTree deletes a key. You have passed a value to be deleted and not a key. If you want to delete a key, use DeleteSubKeyTree, else if you want to delete a value use DeleteValue.
 
Share this answer
 
v2
Hi Snarf80,

You have two errors here:
1. You try to delete another SubKey (not the one you created) LocalMachine/Current User
2. You have to specify the "writable" Parameter when opening the SubKey.

Look at this example I created for you:

using System;
using Microsoft.Win32;
namespace RegistryOhMy
{
    class Program
    {
        static void Main(string[] args)
        {
            string strRootKey = @"SOFTWARE";
            string strSubKey = @"Test";
            string strKeyPath = String.Format(@"{0}\{1}", strRootKey, strSubKey);
            // Create a SubKey            
            RegistryKey regkeyCreate = Registry.LocalMachine.CreateSubKey(strKeyPath);
            regkeyCreate.Close();
            Console.WriteLine("RegistryKey created");
            // Open the created SubKey
            RegistryKey regkeyOpen = Registry.LocalMachine.OpenSubKey(strKeyPath, true);
            if (regkeyOpen != null)
                Console.WriteLine("RegistryKey opened");
            else
                Console.WriteLine("Opening RegistryKey failed");
            // Write a value into the SubKey
            regkeyOpen.SetValue("TestValue", "only a test");
            Console.WriteLine("Test value written");
            
            // Delete a value from the SubKey
            try
            {
                regkeyOpen.DeleteValue("TestValue", true);
                Console.WriteLine("Test value deleted");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while deleting test value: {0}", ex);
            }
            finally
            {
                regkeyOpen.Close();
            }
            // Delete the SubKey
            RegistryKey regkeySoftware = Registry.LocalMachine.OpenSubKey(strRootKey, true);
            regkeySoftware.DeleteSubKeyTree(strSubKey);
            regkeySoftware.Close(); 
            Console.WriteLine("RegistryKey deleted");
            Console.ReadKey();
        }
    }
}
 
Share this answer
 
v2
Comments
johannesnestler 8-Mar-11 18:14pm    
bad vote? whats wrong?
Snarf80 11-Mar-11 11:18am    
@ Ryan Zahra: Thanks for your feedback, but like I stated I'm a newbie not known in programming.
I got far in what I wanted to achieve allready (Got my app. up and running, IO created a new smart device app to integrate use of registry and get stuck here), but without basic programming knowledge I sometimes don't have a clue where or how to fix my errors. So In short your answer did not help me out much further.

@Johannesnestler:
I voted 3 because I'm very happy with your feedback, but it seems to be for a console app.
I tryed quicly to use your code for use in a smartdevice WM6.1/5.0 app. but it gave me an exception error upon running.
The soon I have time to look the code after and applying it in my registry test app, I'll update this tread, wife and kids and work generate a lot of work. This is hobby/work related.
but again, thanks for your feedback.

I'll get back to this after the weekend I think.

Regards,

Frans
johannesnestler 13-Mar-11 6:04am    
Hi Frans,

My code was just thought as an example for you, it shows how to create and delete values and keys. A lot of others would give you just the line you asked for. I like to give complete examples, so you can quickly run it... You showed your code, just correct the typo "Registry.CurrentUser" (should be Registry.LocalMachine) and give the second parameter (writeable=true) when calling OpenSubKey. - If you still can't solve it - let me know, I do a lot of embedded programming...
Snarf80 15-Mar-11 8:29am    
Added underneath as solution 3 but deleted

Johannes Thankz again, for showing complete code..

thanks for pointing my mistake on the localmachine / Currentuser issue

OK tryed several things on deleting subkey subkeytree
Keep getting UnauthorizedAccessException message while the line of deleting the subkey is run.
To test if it's there yes/no works write it, but deleting gives unautorized access

Tested on my laptop running windows 7pro with a windows form app.
The same buttons:

1: write key
2: check key: if it exists: messagebox YES, if NOT message box: NO
3: delete key
Using localmachine it gives an access error.
using current user the underneath code works fine (Windows form app.)

Collapse

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace REGTESTwindows
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btn_RK_create_Click(object sender, EventArgs e)
{
//using Microsoft.Win32;
// Write key "test" with subkey "DeleteMe"
RegistryKey versie1 = Registry.CurrentUser.CreateSubKey("Software\\test\\DeleteMe");
// versie1.SetValue("Version", "1");
versie1.Close();
}

private void btn_RK_present_Click(object sender, EventArgs e)
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey("Software\\test\\DeleteMe");

if (rk != null)
{
MessageBox.Show("YES");// It's there
}
else
{
MessageBox.Show("NO");// It's not there
}
}

private void btn_RK_del_Click(object sender, EventArgs e)
{
// Open SOFTWARE
RegistryKey versie1 = Registry.CurrentUser.OpenSubKey("Software", true);
// Delete "DeleteMe" from SOFTWARE
versie1.DeleteSubKeyTree("test");
//Close the registry SubKey and flush
versie1.Close();
}
}
}

Now if I use the same code for a windows wm5.0 app. it runs but gives me an Unautorized Access Exception error while running the line "versie1.DeleteSubKeyTree("test");"


Collapse

<pre lang="msil">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace RegistryKeys_TEST
{
public partial class Frm_REG_TEST : Form
{
public Frm_REG_TEST()
{
InitializeComponent();
}

private void btn_write_key_Click(object sender, EventArgs e)
{
//using Microsoft.Win32;
// Write key "test" with subkey "DeleteMe"
RegistryKey versie1 = Registry.CurrentUser.CreateSubKey("Software\\test\\Deleteme");
// versie1.SetValue("Version", "1");
versie1.Close();
}
private void btn_rk_exist_Click(object sender, EventArgs e)
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey("Software\\test\\Deleteme");
if (rk != null)
{
MessageBox.Show("YES");// It's there
}
else
{
MessageBox.Show("NO");// It's not there
}
}
private void REG_TEST_L_exit_Click(object sender, EventArgs e)
{
// exit app.
Application.Exit();
}
private void REG_TEST_R_form2_Click(object sender, EventArgs e)
{
// Ga naar Form 2, 2eform kan alles zijn.
Form2 nogeenform = new Form2();
nogeenform.Show();
}
private void btn_del_key_Click(object sender, EventArgs e)
{
{
// Open SOFTWARE
Regis
johannesnestler 3-Apr-11 17:03pm    
your code is truncated... But I will try to test your code when I'm back to work. I think you still have the same error: look at my answer: 2. You have to specify the "writable" Parameter when opening the SubKey. So use the correct overload of OpenSubKey("...", true)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900