|
-
Mar 7th, 2007, 11:22 PM
#1
Thread Starter
New Member
[RESOLVED] [02/03] registry key writes wrong
I'm new to VB.Net, and VB Forums, this is a project for work and I don't get why its not acting correctly. Its supposed to write a value to the registry to restrict certain aspects of the user interface. I wrote this as a mini-proj on how to use the registry. Am I doing this right?
Imports System
Imports Microsoft.Win32
Module Module1
Sub Main()
'Registry Roots
Dim HKCR As RegistryKey = Registry.ClassesRoot 'HKEY_CLASSES_ROOT
Dim HKCC As RegistryKey = Registry.CurrentConfig 'HKEY_CURRENT_CONFIG
Dim HKCU As RegistryKey = Registry.CurrentUser 'HKEY_CURRENT_USER
Dim HKLM As RegistryKey = Registry.LocalMachine 'HKEY_LOCAL_MACHINE
Dim HKU As RegistryKey = Registry.Users 'HKEY_USERS
'subkeys to write to
Const CU_SOFT_POL As String = "SOFTWARE\MICROSOFT\WINDOWS\POLICIES"
Const CU_WIN_POL As String = "SOFTWARE\POLICIES"
Const LM_SOFT_POL As String = "SOFTWARE\POLICIES"
Const LM_WIN_POL As String = "SOFTWARE\MICROSOFT\WINDOWS\POLICIES"
Const Explorer As String = "\EXPLORER"
'reg entries
Const NoTaskbarContextMenu = "NoTrayContextMenu"
Dim KeyName As String
Dim x As Integer
x = 1 '
KeyName = CU_SOFT_POL & Explorer
HKCU.OpenSubKey(KeyName, True)
HKCU.SetValue(KeyName & "\" & NoTaskbarContextMenu, x)
HKCU.Close()
End Sub
End Module
Thanks for any help.
-
Mar 8th, 2007, 01:30 AM
#2
Re: [02/03] registry key writes wrong
This is wrong:
vb Code:
HKCU.OpenSubKey(KeyName, True)
HKCU.SetValue(KeyName & "\" & NoTaskbarContextMenu, x)
It should be:
vb Code:
Dim regKey As RegistryKey = HKCU.OpenSubKey(KeyName, True)
regKey.SetValue(NoTaskbarContextMenu, x)
regKey.Close()
-
Mar 8th, 2007, 06:58 PM
#3
Thread Starter
New Member
Re: [02/03] registry key writes wrong
Thank you. That helped a lot. I think that I understand what I did wrong. I just wasn't opening the key correctly.
I need to change this into a function that will modify the registry and let me know if it was successful. Is there an easier way to check for your key than this:
Code:
Private Function RegSV( _
ByVal Key As String, _
ByVal name As String, _
ByVal v As Object) As Integer
Dim KeyNames() As String
Dim rkey As RegistryKey = Registry.CurrentUser.OpenSubKey(Key, True)
rkey.SetValue(name, v)
'how to determine if succesful?
KeyNames = rkey.GetValueNames()
rkey.Close()
'0 = success, 1 = failed
RegSV = 1
For p As Integer = 0 To UBound(KeyNames)
If name = KeyNames(p) Then
RegSV = 0
Exit For
End If
Next
End Function
-
Mar 8th, 2007, 07:15 PM
#4
Re: [02/03] registry key writes wrong
There's already a method that does that. You can call the Shared Registry.SetValue method and specify a key (including hive), name and value. You'd need to wrap it in an exception handler and if an exception is thrown then it failed, otherwsie it succeeded.
Also, don't use Integers like that. If you want to indicate whether something succeeded or failed use a Boolean.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|