C#删除注册表项,无法完成 [英] C# delete a registry key, cannot get it done

查看:202
本文介绍了C#删除注册表项,无法完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试删除Windows Mobile中的注册表项

我尝试在google上查找代码
^ ]
提供了很多信息,但并不能帮助我解决它.

我试过了
http://www.codeproject.com/KB/system/modifyregistry.aspx
但是没有让它奏效


我写了一个钥匙,检查那里是否有钥匙
但是删除时会出现异常.

我正在使用Visual Studio 2008 Pro编写应用程序.适用于C#中的智能设备

我是编程新手,但可以通过学习家乡和学习网站,复制和编辑Internet(msdn,codeproject,google等)提供的代码来启动和运行应用程序.

我想使用注册表,所以我创建了一个测试应用程序.测试写入/读取和删除注册表项/值.

有没有人可以提供有效的代码?

如果方便,我可以提供源文件(项目)吗?

预先感谢

下方的代码:
Button1:写入密钥
Button2:检查密钥是否存在
Button3应该删除密钥.

我想我需要一个过载器或其他东西
(我不知道重载是什么,我不是程序员,
试图到达某个地方,但观看,使用,修改并因此学习)


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();
        }


        }
    }








问候,

Frans








Regards,

Frans

推荐答案

DeleteSubKeyTree删除密钥.您传递了要删除的值而不是键.如果要删除键,请使用DeleteSubKeyTree,否则,请使用DeleteValue.
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.


Snarf80,

您在这里有两个错误:
1.您尝试删除另一个SubKey(不是您创建的一个)LocalMachine/Current User
2.打开SubKey时必须指定可写"参数.

看看我为您创建的以下示例:

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();
        }
    }
}


这篇关于C#删除注册表项,无法完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆