Page 1 of 1

Working with HKLM and HKCU startups in VB.NET Rate Topic: -----

#1 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 30 June 2008 - 01:03 PM

Just as there is a startup folder for all users and the current user, there are startups in the registry as well. The startups in the registry start before the ones in the startup folders. They are located in two places. The equivalent of the user startup folder in the registry is in HKEY_CURRENT_USER. Items in that registry startup start only for that user. The equivalent of the all user startup folder is the HKEY_LOCAL_MACHINE startup, which start for all users. There are also RunOnce startups in each of those places as well. A RunOnce startup will, as it's name says, run only on the next reboot, and will be deleted as a startup after that.

HKCU startups

The HKCU startups are located in a registry key under the HKCU base key. The exact path of that key is HKCU\Software\Microsoft\Windows\CurrentVersion\Run\. To access that registry key in your code, use

My.Computer.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft") _
.OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Run")


That will return a Microsoft.Win32.RegistryKey that equals the HKCU run key.

HKCU RunOnce

The HKCU run once's are located in almost the same place as the normal startups, the only only difference is they are located in a key called RunOnce instead of Run. The full path of the HKCU runonce is HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce\.

HKLM startups

The HKLM startups are located in the same path at the HKCU startups, except they are under the base key HKLM. The full path of the HKLM startups is HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\. To access that key in your code, use

My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft") _
.OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Run")



The HKLM starups can be accessed and edited in the same way as the HKCU startups, the only difference between them is that they one is all user and one is current user. We will talk about how to edit registry keys later.

HKLM RunOnce

The HKLM run once's can be access in the same way as the HKCU run once's. The full path of the HKLM run once's is HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\.

HKLM RunServices

This is something we have not talked about yet. Unlike the HKCU key, the HKLM key has a RunServices key. The path of the RunServices key is HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices\. Don't confuse services with standard startups, because they are different, and for someone who is just learning about registry startups, you probably shouldn't mess with Services until you know a bit more about them. Learn more about services here.

Editing the Microsoft.Win32.RegistryKey class

There are many methods in the registrykey class. Here, we will only talk about the ones that could possibly be of use when the registrykey is a startup key. These methods are

SetValue

Usage: This can be used to edit an existing startup
Syntax:
RegistryKeyName.SetValue(StartupName As String, StartupPath As String)



GetValue

Usage: This can be used to get a path to an existing startup
Syntax:
RegistryKeyName.GetValue(StartupName As String) As Object



ValueCount (property)

Usage: Read-only property that shows how many startups are in the startup key
Syntax:
RegistryKeyName.ValueCount() As Integer



GetValueNames

Usage: This can be used to get the names of all the startups in the startup key
Syntax:
RegistryKeyName.GetValueNames() As String()



DeleteValue

Usage: This can be used to delete an existing startup
Syntax:
RegistryKeyName.DeleteValue(StartupName As String)



Is This A Good Question/Topic? 0
  • +

Replies To: Working with HKLM and HKCU startups in VB.NET

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 30 June 2008 - 10:48 PM

Good Job.
But just so you know, you don't need to use the OpenSubKey() method everytime.

My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run")

is totally valid and a lot more readable than
My.Computer.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft") _
.OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Run")

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1