Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working wit h the registry

Status
Not open for further replies.

MoleDirect

Technical User
Sep 4, 2007
45
US
Hello all... I am working on a project that will have to access the local computers registry to get values, and report on them. And if need be, change them... Here is the code / theory I am working on..

Code:
If 
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoLowDiskSpaceChecks"=dword:00000001

returns true then

doesitwork.text = " It works " 

Else


doesitwork.text = "It doesn't work, you are a lousy coder! " 

end if
 ' -----------------------------------------
SUB (button handler script) onclick
 'this button would make the value 1

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoLowDiskSpaceChecks"=dword:00000001

end sub
 '------------------------------------------
SUB (button handler script) onclick
 'this button would make the value 0

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoLowDiskSpaceChecks"=dword:00000000

end sub

Now I know the above code does not work, but hopefully, someone will have a working snippet that does work... When it comes to the registry, i don't even know where to start..

Thanks in advance!!
 
Actually, I did try that, and most of the snippits i found didnt work.

I am trying to work with this code, but not doing well:

Code:
Microsoft.Win32.Registry
 
The link sent me to a google in another language, and i did try most of those results, but could not find anything that works.
 
Is there another version then the belgian one?

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
This is what I am up to so far in the code:


Code:
        Dim instance As Microsoft.Win32.RegistryKey
        Dim returnValue As String()
        Dim query As Microsoft.Win32.Registry
        returnValue = instance.GetValue(jackass)
        query.CurrentUser.Name

And what I want it to pretty much end with is

Code:
        If Microsoft.Win32.RegistryValueOptions("Software\Microsoft\Windows\CurrentVersion\Internet Settings\NoLowDiskSpaceChecks" = true) Then
            Panel3.BackColor = Color.Black
            Panel4.BackColor = Color.LimeGreen
        Else
            Panel3.BackColor = Color.LimeGreen
            Panel4.BackColor = Color.Black
        End If
 
I am bumping thread because i have not been able to resolve :)
 
I wrote a tutorial on working with the Windows Registry (on another site so hopefully I don't get anyone upset for posting the link)

Working with the Windows Registry in VB.Net

Application Developer: VB.Net, Qik III, SQL 2000/2005

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Let's fact it, most computer users have the brain of a Spider Monkey"
~ Bill Gates ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Im Making progress!! Thank you PsychoCoder!!

I have the class installed, but I am at a loss how to apply it. :/ I am a true m0e when it comes to this...

Here is the clas file as i have it:
Code:
Imports Microsoft.Win32
Imports System.Windows.Forms
'*****************************************************************************************
'                           LICENSE INFORMATION
'*****************************************************************************************
'   PCRegistryClass Version 1.0.0.3
'   Creates a CAPTCHA (security) image to help prevent "bot spamming" and other data
'   input into online web systems via a software "bot"
'
'   Copyright (C) 2005  
'   Richard L. McCutchen 
'   Email: richard@psychocoder.net
'   Created: 23JUN05
'
'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program.  If not, see <[URL unfurl="true"]http://www.gnu.org/licenses/>.[/URL]
'*****************************************************************************************
Namespace PC
    Public Class PCRegistryClass

#Region " Class Methods "
#Region " Writing Methods "
        ''' <summary>
        ''' Function to create a new SubKey in the Windows Registry
        ''' </summary>
        ''' <param name="MainKey">RegistryKey -> One of the 6 main keys to create the subkey in</param>
        ''' <param name="sKey">String -> Name of the subkey to create</param>
        ''' <param name="KeyPermissions">RegistryKepPermissionCheck -> Specifies permissions of the SubKey to be created</param>
        ''' <returns>True (Succeeded)/False (Failed)</returns>
        ''' <remarks>Created 23JUN05 -> Richard L. McCutchen</remarks>
        Public Function CreateRegistrySubKey(ByVal MainKey As RegistryKey, ByVal sKey As String, _
        ByVal KeyPermissions As RegistryKeyPermissionCheck) As Boolean
            Try
                'create the new subkey
                MainKey.CreateSubKey(sKey, KeyPermissions)
                Return True
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Creating SubKey", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End Try
        End Function

        ''' <summary>
        ''' Writes a value in the Registry
        ''' </summary>
        ''' <param name="MainKey">RegistryKey -> One of the 6 main keys that you want to write to</param>
        ''' <param name="sKey">String -> Name of the subkey you want to write to. If the subkey doesnt
        ''' exist it will be created</param>
        ''' <param name="sKeyName">String -> Name of the value to create</param>
        ''' <param name="oNameValue">Object -> Value to be stored</param>
        ''' <param name="RegType">RegistryValueKind -> Data type of the subkey value</param>
        ''' <returns>True (Succeedeed)/False (Failed)</returns>
        ''' <remarks>Created 23JUN05 -> Richard L. McCutchen</remarks>
        Public Function WriteSubKeyValue(ByVal MainKey As RegistryKey, ByVal sKey As String, ByVal sKeyName As String, _
                                   ByVal oNameValue As Object, ByVal RegType As RegistryValueKind) As Boolean
            Dim rkKey As RegistryKey
            Try
                'Open the given subkey
                rkKey = MainKey.OpenSubKey(sKey, True)
                'check to see if the subkey exists
                If rkKey Is Nothing Then 'doesnt exist
                    'create the subkey
                    rkKey = MainKey.CreateSubKey(sKey, RegistryKeyPermissionCheck.Default)
                End If
                'set the value of the subkey
                rkKey.SetValue(sKeyName, oNameValue, RegType)
                Return True
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Writing Registry Value", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End Try
        End Function
#End Region

#Region " Reading Methods "
        ''' <summary>
        ''' Function to read a value from the Registry
        ''' </summary>
        ''' <param name="MainKey">RegistryKey -> One of the 6 main level keys you want to write to</param>
        ''' <param name="sKey">String -> Sub key you want to read</param>
        ''' <param name="sKeyName">String -> Name of the value you want to read</param>
        ''' <param name="oNameValue">Object -> The value to be read</param>
        ''' <returns>True (Succeeded)/False (Failed)</returns>
        ''' <remarks>Created 23JUN05 -> Richard L. McCutchen</remarks>
        Public Function ReadRegistryValue(ByVal MainKey As RegistryKey, ByVal sKey As String, ByVal sKeyName As String, _
                                  ByRef oNameValue As Object) As String
            Dim rkKey As RegistryKey
            Dim Value As New String("")
            Try
                'open the given subkey
                rkKey = MainKey.OpenSubKey(sKey, True)
                'check to see if the subkey exists
                If rkKey Is Nothing Then 'it doesnt exist
                    'throw an exception
                    Throw New Exception("The Registry SubKey provided doesnt exist!")
                End If
                'get the value
                oNameValue = rkKey.GetValue(sKeyName)
                Value = oNameValue.ToString
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Reading Registry Value", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            Return Value
        End Function
#End Region

#Region " Deleting Methods "
        ''' <summary>
        ''' Function to delete a subkey value from the Windows Registry
        ''' </summary>
        ''' <param name="MainKey">RegistryKey -> One of the 6 main keys you want to delete from</param>
        ''' <param name="sKey">String -> Name of the SubKey you want to delete a value from</param>
        ''' <param name="sKeyName">String -> Name of the value to delete</param>
        ''' <returns>True (Succeeded)/False (Failed)</returns>
        ''' <remarks>Created 23JUN05 -> Richard L. McCutchen</remarks>
        Public Function DeleteSubKeyValue(ByVal MainKey As RegistryKey, ByVal sKey As String, ByVal sKeyName As String) As Boolean
            Dim rkKey As RegistryKey
            Try
                'open the given subkey
                rkKey = MainKey.OpenSubKey(sKey, True)
                'check to make sure the subkey exists
                If Not sKey Is Nothing Then 'subkey exists
                    'delete the subkey
                    MainKey.DeleteValue(sKeyName, True)
                    Return True
                Else    'subkey doesnt exist
                    'throw an exception
                    Throw New Exception("The SubKey provided doesnt exist! Please check your entry and try again")
                    Return False
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Deleting SubKey Value", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End Try
        End Function

        ''' <summary>
        ''' Function to delete a SubKey from the Windows Registry
        ''' </summary>
        ''' <param name="MainKey">RegistryKey -> One of the top main keys to delete from</param>
        ''' <param name="sKey">String -> Name of the SubKey to delete</param>
        ''' <returns>True (Succeeded)/False (Failed)</returns>
        ''' <remarks>Created 23JUN05 -> Richard L. McCutchen</remarks>
        Public Function DeleteRegistrySubKey(ByVal MainKey As RegistryKey, ByVal sKey As String) As Boolean
            Dim rkKey As RegistryKey
            Try
                'open the given subkey
                rkKey = MainKey.OpenSubKey(sKey, True)
                'check to make sure the subkey exists
                If Not sKey Is Nothing Then 'subkey exists
                    MainKey.DeleteSubKey(sKey, True)
                    Return True
                Else    'subkey doesnt exist
                    'throw an exception letting the user know
                    Throw New Exception("The SubKey provided doesn't exist. Please check your entry and try again.")
                    Return False
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Deleting SubKey", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End Try
        End Function
#End Region





       
#End Region
    End Class
End Namespace


And I have that in a seprate class.vb file in the code... Now, in the main form, I am hoping to have an 'indicator light' show weather or not a reg key is set to 1 or 0.

Here is the 'indicator light' code
Code:
     If Microsoft.Win32.RegistryValueOptions("Software\Microsoft\Windows\CurrentVersion\Internet Settings\NoLowDiskSpaceChecks" = true) Then
            Panel3.BackColor = Color.Black
            Panel4.BackColor = Color.LimeGreen
        Else
            Panel3.BackColor = Color.LimeGreen
            Panel4.BackColor = Color.Black
        End If

I will have an ON and OFF button that will change the registry, wait a few seconds, then update the 'indicator light'..

Can you give an example with reading the following registry entry and how to use the CLASS file??

Here is the reg
Code:
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoLowDiskSpaceChecks"=dword:00000000
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top