Click here to Skip to main content
14,828,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");


code for class unin

public class unins


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
Posted
Updated 9-Apr-12 7:36am
v3
Comments
[no name] 9-Apr-12 13:08pm
   
Format code snippets when posting a question

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
   
Comments
VJ Reddy 9-Apr-12 13:24pm
   
Good suggestion. 5!
   
Thank you, VJ.
--SA
Rajesh Thennan 9-Apr-12 13:45pm
   
Hi SA, to be honest with you, "Read, write and delete from registry with C#[^]" didn't quite have the answer for my question.

and as far as http://msdn.microsoft.com/en-us/library/xthy8s8d.aspx[^] is concerned, I am quite new to C#. I am undergoing a C# tarining course whihc is just teaching me the OOPS concepts in C#. And the trainer, gave me a weird and blank look when I told him that I was trying to work on the registry using C#.

And I personally feel that its more important to understand the code than to blindly implement it.

So, with all due respect, thanks.

But, OpenSubKey cannot be returning null. Cause I manullay created the "testvalue" as I did not want to mess around with the legit keys and values which I wanted to deleted finally.

And I am pretty sure there cannot be a permission issue as I have given full permissions for everyone for the hklm/software subkey. I have also disabled my windows 7 UAC just in case.

Thanks in advance
Sergey Alexandrovich Kryukov 10-Apr-12 10:57am
   
In principle, it can. I clearly told you this is a first candidate for this exception. Finding exception by your code is wrong approach. The right approach is running it under debugger, which only you can do in this situation. I told you exactly, what to do. So, that is a real answer to your question, not matter if you realized this or not. For you, it's the best to realize it and do exactly this.

I really appreciate your effort to really understand things. I think one should fully understand every line one writes instead of using cookbook recipes. This is yet another reason I insist on debugging session. You pinpoint the problem first, and, if its nature is not 100% clear to you, we can discuss it.

Now, as to instead if disabling UAC, I would advice you run the code with elevated privileges instead. Here are all the options you have:
http://www.sevenforums.com/tutorials/11841-run-administrator.html

This issue is not permissions, quite apparently. This is null appearing where non-null is expected, nothing else. This is one of the easiest issues to detect. Jut do it.
--SA
Monjurul Habib 10-Apr-12 1:13am
   
5!
Sergey Alexandrovich Kryukov 10-Apr-12 10:49am
   
Thank you, Monjurul.
--SA
Rajesh Thennan 10-Apr-12 16:41pm
   
Hi SA, first of all, really sorry for replying without fully understanding your solution. I might be doing the "debugging aprt" wrong. As mentioned already, I created a key called "test" just under "HKEY_LOCAL_MACHINE\SOFTWARE". and created a value called "testvalue" in it. so, we know that its not "null". but how do I find out if

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(keyname, true)) is returning null or not?

I may sound silly. but sometimes, there are more than one way to reach to a solution for a problem. And I shoudl accept that I did not come up with the above solution and was just one "cookbook recipie". so is there a better way or function which can be used to accomplish the requirement? or is there a btter one?

to put the requirement accross again, a value called "testvalue" of subkey under HKEY_LOCAL_MACHINE. the subkey in this case is HKEY_LOCAL_MACHINE\SOFTWARE\test . I am blank slate and be my guest for writing on me.
Sergey Alexandrovich Kryukov 12-Apr-12 12:22pm
   
Thank you for understanding. And no, if you do no fully understand something, you should not be sorry about replying -- this is right thing to do.
So, let's look at OpenSubKey:
http://msdn.microsoft.com/en-us/library/xthy8s8d.aspx
If clearly indicates that it can return null, but I feel that that this documentation page was only recently fixed: for C#, it spelled out "null" in term of VB.NET, as "Nothing". Look again, right now it looks correct. For C++/CLI, this is managed pointer "nullptr", etc.

You really need to go with the debugger to see what's going on; near the location of you problem it should be reviewed on step-by-step level. If you do it, you will easily see what's wrong.
--SA
I think this Code Project article
Read, write and delete from registry with C#[^]
may be helpful.
   
Comments
   
Could be helpful in some situations, but I feel OP basically knows all that. The problem is just general development and debugging skills. (I voted 4.)
--SA
VJ Reddy 9-Apr-12 13:35pm
   
Thanks SA. I like your in depth analysis of question and elaborate, comprehensive and specific answers.
Sergey Alexandrovich Kryukov 12-Apr-12 12:13pm
   
Thank you very much for your good words.
--SA
Rajesh Thennan 9-Apr-12 13:48pm
   
Hi VJ Reddy, thanks for the suggestion, but http://msdn.microsoft.com/en-us/library/xthy8s8d.aspx gives options of setting and getting values but not for deleting a value totally. I can set the value's description to "0" or null. but that wouldn't serve the purpose. for a clean uninstall, the "testvalue" has to be deleted completely from the "test" sub key. To put it across in simple, a value called "testvalue" has to be deleted from the subkey "HKEY_LOCAL_MACHINE\SOFTWARE\test" . The key exists; the value exists; and no permission issues.
Monjurul Habib 10-Apr-12 1:13am
   
5!
VJ Reddy 10-Apr-12 1:17am
   
Thank you.

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