Home All Groups Group Topic Archive Search About

Newbie Question - Please HELP!

Author
8 Apr 2005 1:02 AM
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

Author
8 Apr 2005 1:29 AM
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
Author
8 Apr 2005 2:18 AM
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


Show quoteHide quote
"Crouchie1998" <crouchie1998@spamcop.net> wrote in message
news:ebVIKq9OFHA.3356@TK2MSFTNGP12.phx.gbl...
> 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
>
>
Author
8 Apr 2005 1:16 PM
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
Author
8 Apr 2005 1:54 PM
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


Show quoteHide quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1112966193.164941.258970@z14g2000cwz.googlegroups.com...
> 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
>
Author
8 Apr 2005 2:10 PM
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


Show quoteHide quote
"mikew" <thewitt***@yahoo.com> wrote in message
news:%23EuoNKEPFHA.3668@TK2MSFTNGP14.phx.gbl...
> 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
>
>
> "Chris Dunaway" <dunaw***@gmail.com> wrote in message
> news:1112966193.164941.258970@z14g2000cwz.googlegroups.com...
>> 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
>>
>
>
Author
8 Apr 2005 4:44 PM
Crouchie1998
You could just do this:

If Not Reg Is Nothing Then reg.Close()

Crouchie1998
BA (HONS) MCP MCSE
Author
8 Apr 2005 7:31 PM
mikew
I don't think that solves the problem....
Thanks,
m

Show quoteHide quote
"Crouchie1998" <crouchie1998@spamcop.net> wrote in message
news:%23DU8mpFPFHA.3988@tk2msftngp13.phx.gbl...
> You could just do this:
>
> If Not Reg Is Nothing Then reg.Close()
>
> Crouchie1998
> BA (HONS) MCP MCSE
>
>
Author
8 Apr 2005 8:58 PM
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
Author
8 Apr 2005 9:07 PM
Crouchie1998
Forgot to add. You will need the following Import statements:

Imports Microsoft.win32
Imports System.Security.Permissions

Crouchie1998
BA (HONS) MCP MCSE

"Crouchie1998" <crouchie1998@spamcop.net> wrote in message
news:en16c3HPFHA.2824@TK2MSFTNGP10.phx.gbl...
> 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
Author
8 Apr 2005 10:08 PM
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



Show quoteHide quote
"Crouchie1998" <crouchie1998@spamcop.net> wrote in message
news:OKFKU8HPFHA.164@TK2MSFTNGP12.phx.gbl...
> Forgot to add. You will need the following Import statements:
>
> Imports Microsoft.win32
> Imports System.Security.Permissions
>
> Crouchie1998
> BA (HONS) MCP MCSE
>
> "Crouchie1998" <crouchie1998@spamcop.net> wrote in message
> news:en16c3HPFHA.2824@TK2MSFTNGP10.phx.gbl...
>> 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
>
>
Author
8 Apr 2005 10:27 PM
Crouchie1998
I see your problem

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

Crouchie1998
BA (HONS) MCP MCSE
Author
9 Apr 2005 4:20 AM
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

Show quoteHide quote
"Crouchie1998" <crouchie1998@spamcop.net> wrote in message
news:uEUJGpIPFHA.3076@tk2msftngp13.phx.gbl...
>I see your problem
>
> DeleteRegistrySubKeyTree("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer
> ", "VolumeCaches")
>
> Crouchie1998
> BA (HONS) MCP MCSE
>
>