How to get the registry key permission using RegistryRight in NET2.0?

  • Thread starter Thread starter yxq
  • Start date Start date
Y

yxq

Hello,
I want to get the registry key permission information whether current user
has the permission to delete the registry key using RegistryRights, how to
do? thank you.
 
Public Sub AccessRegistry(ByVal rk As RegistryKey, ByVal sKey As
String, ByVal action As RegistryPermissionAccess)
Dim rp As New RegistryPermission(PermissionState.None)
rp.SetPathList(action, rk.Name)
rp.Demand()

Select Case action
Case RegistryPermissionAccess.Create
rk.CreateSubKey("Third Sector Technologies")
Case RegistryPermissionAccess.Read
Dim sk As RegistryKey = rk.OpenSubKey(sKey)
Dim sValue As String = sk.GetValue("Floyd")
Case RegistryPermissionAccess.Write
Dim sk As RegistryKey = rk.OpenSubKey(sKey)
sk.SetValue("Floyd", 1)
End Select
End Sub

Call this code like this:

Dim rk As RegistryKey = Registry.CurrentUser
AccessRegistry(rk, "Third Sector Technologies",
RegistryPermissionAccess.Create)
 
Also

Function RegistryRetrieve(parent As RegistryKey, keyName As String,
valueName As String) As String
' Recursively search through each subkey of a parent key
' checking to see if a match for the keyname exist and if
' the data can be obtained from that key. Returns data
upon
' match and an error message otherwise.
Dim childName As String
For Each childName In parent.GetSubKeyNames()
Try
Dim child As RegistryKey =
parent.OpenSubKey(childName)
Dim data As String = Nothing
If child.Name.EndsWith(keyName) Then
' We found the name, try to pull out key
data
data = child.GetValue(valueName)
Else
' No luck finding the name, try a
depth-first recursive search
data = RegistryRetrieve(child, keyName,
valueName)
End If
If Not (data Is Nothing) Then
Return data
End If
Catch ex As Exception
' Print out a message whenever we come
across a
' key we don't have permissions to open
Console.WriteLine((childName + ": " +
ex.Message))
End Try
Next childName


Return Nothing
End Function
 
Thank you.

vbnetdev said:
Also

Function RegistryRetrieve(parent As RegistryKey, keyName As String,
valueName As String) As String
' Recursively search through each subkey of a parent key
' checking to see if a match for the keyname exist and
if
' the data can be obtained from that key. Returns data
upon
' match and an error message otherwise.
Dim childName As String
For Each childName In parent.GetSubKeyNames()
Try
Dim child As RegistryKey =
parent.OpenSubKey(childName)
Dim data As String = Nothing
If child.Name.EndsWith(keyName) Then
' We found the name, try to pull out
key data
data = child.GetValue(valueName)
Else
' No luck finding the name, try a
depth-first recursive search
data = RegistryRetrieve(child, keyName,
valueName)
End If
If Not (data Is Nothing) Then
Return data
End If
Catch ex As Exception
' Print out a message whenever we come
across a
' key we don't have permissions to open
Console.WriteLine((childName + ": " +
ex.Message))
End Try
Next childName


Return Nothing
End Function
 
Back
Top