Newbie Question - Please HELP!

M

mikew

The following code isn't working and I don't understand the error. Would
someone please shed some light on what I'm doing wrong. Thanks.
The error I recieve is:
An unhandled exception of type 'System.NullReferenceException'
occurred....additional information: Object reference not set to an instance
of an object.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
key.DeleteSubKeyTree("TestTree1")
End Sub

Thanks,
mike
 
C

Crouchie1998

Try this:

Imports Microsoft.win32

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim reg As RegistryKey

reg = Registry.LocalMachine.OpenSubKey("Software", True)

If Not (reg Is Nothing) Then
reg.DeleteSubKeyTree("TestTree1")
End If

reg.Close()

End Sub

This works perfectly for me

Crouchie1998
BA (HONS) MCP MCSE
 
M

mikew

I see. That does work. Thanks.
But why when I change "Software" to "VolumeCaches" do I get the "object
reference not set to an instance of an object" on the reg.close() line -
even when testtree1 is a valid key under VolumeCaches?

-mike
 
C

Chris Dunaway

If the key does not exist, reg will be Nothing so that when you hit the
..close, you are trying to call a method with a Nothing object. Place
the .close inside the if block where you check for nothing:

If Not (reg Is Nothing) Then
reg.DeleteSubKeyTree("TestTree1")
rec.Close
End If
 
M

mikew

That makes sence, but why doesn't the code remove the key when
reg = Registry.LocalMachine.OpenSubKey("VolumeCaches", True)

I create a key named TestTree1 in VolumeCaches and then run the code and it
doesn't remove anything....is there something special about "VolumeCaches"
that makes it's subkey's not removable by the code? I can manually remove
keys from this location without issue....

-m
 
M

mikew

just to clsrify the current issue...this works:

----------------------------------------------------
Dim reg As RegistryKey
reg = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
If Not (reg Is Nothing) Then
reg.DeleteSubKeyTree("TestTree1")
reg.Close()
End If
--------------------------------------------------------

but this doesn't work:


Dim reg As RegistryKey
reg = Registry.LocalMachine.OpenSubKey("VolumeCaches", True)
If Not (reg Is Nothing) Then
reg.DeleteSubKeyTree("TestTree1")
reg.Close()
End If
----------------------------------------------------------------------------

TestTree1 exists in both subkeys.
I recieve no error however the key is not removed under scenario 2.
Imports Microsoft.Win32 is being referenced in both cases.
I apprecieate everyone's help thusfar.
Thanks,
mike
 
C

Crouchie1998

You could just do this:

If Not Reg Is Nothing Then reg.Close()

Crouchie1998
BA (HONS) MCP MCSE
 
C

Crouchie1998

You can try this:

Private Function DeleteRegistrySubKeyTree(ByVal sKey As String, ByVal
sSubkey As String) As Boolean

Dim reg As RegistryKey

Dim rp As New RegistryPermission(RegistryPermissionAccess.AllAccess, sKey)
rp.Assert()

Try
reg = Registry.LocalMachine.OpenSubKey(sKey, True)
If Not (reg Is Nothing) Then
reg.DeleteSubKeyTree(sSubkey)
End If
Catch ex As Exception
Return False

Finally

If Not reg Is Nothing Then reg.Close()
rp.RevertAssert()
rp = Nothing
End Try

Return True

End Function

Usage:
-------

1) DeleteRegistrySubKeyTree("Software", "TestTree1")

2)

If DeleteRegistrySubKeyTree("Software", "TestTree1") = True Then
MessageBox.Show("Sub Key Tree Deleted Successfully", Me.Text)
Else
MessageBox.Show("Sub Key Tree Deletion failed", Me.Text)
End If

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
 
C

Crouchie1998

Forgot to add. You will need the following Import statements:

Imports Microsoft.win32
Imports System.Security.Permissions

Crouchie1998
BA (HONS) MCP MCSE

Crouchie1998 said:
You can try this:

Private Function DeleteRegistrySubKeyTree(ByVal sKey As String, ByVal
sSubkey As String) As Boolean

Dim reg As RegistryKey

Dim rp As New RegistryPermission(RegistryPermissionAccess.AllAccess, sKey)
rp.Assert()

Try
reg = Registry.LocalMachine.OpenSubKey(sKey, True)
If Not (reg Is Nothing) Then
reg.DeleteSubKeyTree(sSubkey)
End If
Catch ex As Exception
Return False

Finally
If Not reg Is Nothing Then reg.Close()
rp.RevertAssert()
rp = Nothing
End Try

Return True

End Function

Usage:
-------

1) DeleteRegistrySubKeyTree("Software", "TestTree1")

2)

If DeleteRegistrySubKeyTree("Software", "TestTree1") = True Then
MessageBox.Show("Sub Key Tree Deleted Successfully", Me.Text)
Else
MessageBox.Show("Sub Key Tree Deletion failed", Me.Text)
End If

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
 
M

mikew

Does anyone have a clue why this fails when you pass VolumeCaches as the
sKey.
As in...DeleteRegistrySubKeyTree("VolumeCaches", "TestTree1")
When "TestTree1" is a legit key under VolumeCaches.

I thought Crouchie hit it with the System.Security.Permissions references,
but it still isn't working when the target is under
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches

Thanks,
Mike
 
C

Crouchie1998

I see your problem

DeleteRegistrySubKeyTree("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer
", "VolumeCaches")

Crouchie1998
BA (HONS) MCP MCSE
 
M

mikew

Thanks...your help was helpful... :)
This worked.

DeleteRegistrySubKeyTree("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches",
"TestTree1")

I thought I had tried that iteration but aparently not.

Thanks again,
m
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top