Results 1 to 4 of 4

Thread: [RESOLVED] [02/03] registry key writes wrong

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    12

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] registry key writes wrong

    This is wrong:
    vb Code:
    1. HKCU.OpenSubKey(KeyName, True)
    2. HKCU.SetValue(KeyName & "\" & NoTaskbarContextMenu, x)
    It should be:
    vb Code:
    1. Dim regKey As RegistryKey = HKCU.OpenSubKey(KeyName, True)
    2.  
    3. regKey.SetValue(NoTaskbarContextMenu, x)
    4. regKey.Close()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    12

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width