无法使用C#删除注册表值 [英] Unable to delete registry Value(s) using C#

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

问题描述

大家好,

我是C#的新手.我正在尝试开发一个卸载程序,需要停止一个过程,删除一些文件夹和文件,删除一些注册表项,最后删除一些注册表项的值.我已经完成了所有其他工作,但是在删除注册表值时遇到了问题.

为了测试应用程序的功能,我在HKEY_LOCAL_MACHINE \ SOFTWARE下创建了一个名为"test"的新密钥.因此,密钥的名称为HKEY_LOCAL_MACHINE \ SOFTWARE \ test.

我在其中创建了一个名为"testvalue"的值

在名为"unins"的主类中,我创建了一个名为"key1"的方法.

Hi all,

I am new to C#. I am trying to develop an uninstaller application which requires stopping a process, deleting a few folders and files, deleting a few registry keys and at last deleting a few values of a a registry key. I have achieved everything else but have problems in deleting the registry values.

To test the application''s functionality, I have created a new key called "test" under HKEY_LOCAL_MACHINE\SOFTWARE . So, the key''s name is HKEY_LOCAL_MACHINE\SOFTWARE\test.

And I have created a value in it called "testvalue"

In the main class which I have named "unins", I have created a method called "key1".

public string key1(string keyname, string val)
    {
      using (RegistryKey key = Registry.LocalMachine.OpenSubKey(keyname, true))
      key.DeleteValue(val);
      return "value "+val+" Deleted";
        }



在按钮单击事件中,我尝试调用如下方法:



and in the button click event, I have tried calling the method like this :

unins archi =new unins();
textBox1.Text = archi.key1(@"SOFTWARE/test, "testvalue");



unin类的代码



code for class unin

public class unins



我在类中使用了一些其他方法来删除文件和文件夹,但仅在删除注册表值部分时才得到例外.


但是我得到对象引用未设置为对象的瞬间"

我可能做错了.

因此,请让我知道如何使用C#删除此特定值.

在此先感谢
Rajesh T



I have ussed a few more methods in the class for file and folder deletion but I get the exception only for the deleting registry value part of it.


But I get an "object reference not set to an instant of an obejct"

I might be doing it wrong.

So please let me know How I can delete this specific value, using C#.

Thanks in Advance
Rajesh T

推荐答案

方法OpenSubKey可以返回null,因此这是此异常的第一个候选对象.请参阅:
http://msdn.microsoft.com/en-us/library/xthy8s8d.aspx [ ^ ].

另外,您没有显示unins的代码,因此我无法确定异常是否存在.或其他任何地方.

不要担心.这种异常最容易检测到.您只需要使用调试器.引发异常时,在该类上放置一个断点,然后再次运行该应用程序.在异常即将发生之前,将在异常将要抛出的位置停止执行.检查下一行中使用的每个值.出现异常的原因是某些不应该为null的值实际上为null.您将很快找到它.

通常,在问像这样的问题之前,您需要使用调试器.实际上,您需要使用调试器,以防万一您担心运行时行为.

祝你好运,
—SA
The method OpenSubKey can return null, so this is the first candidate for this exception. Please see:
http://msdn.microsoft.com/en-us/library/xthy8s8d.aspx[^].

Also, you did not show the code for unins, so I cannot tell if the exception is there. Or anywhere else.

Not to worry. This kind of exception is easiest to detect. You just need to use the debugger. When the exception is thrown, put a break point on this like and run the application again. The execution will be stopped at the point where the exception is about to throw, just before it happens. Examine every value used in next line. The reason of the exception is that some value which is not supposed to be null is actually null. You will find it in no time.

Generally, you need to use the debugger before asking questions like this one. Actually, you need to use the debugger in case of slightest concern about your run time behavior.

Good luck,
—SA


我认为此代码项目文章
使用C#从注册表中读取,写入和删除 [ ^ ]
可能会有所帮助.
I think this Code Project article
Read, write and delete from registry with C#[^]
may be helpful.


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

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