Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,764 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,631 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Working with HKLM and HKCU startups in VB.NET

 
Reply to this topicStart new topic

> Working with HKLM and HKCU startups in VB.NET

jacobjordan
Group Icon



post 30 Jun, 2008 - 01:03 PM
Post #1


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

vb

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

vb

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:
vb

RegistryKeyName.SetValue(StartupName As String, StartupPath As String)


GetValue

Usage: This can be used to get a path to an existing startup
Syntax:
vb

RegistryKeyName.GetValue(StartupName As String) As Object


ValueCount (property)

Usage: Read-only property that shows how many startups are in the startup key
Syntax:
vb

RegistryKeyName.ValueCount() As Integer


GetValueNames

Usage: This can be used to get the names of all the startups in the startup key
Syntax:
vb

RegistryKeyName.GetValueNames() As String()


DeleteValue

Usage: This can be used to delete an existing startup
Syntax:
vb

RegistryKeyName.DeleteValue(StartupName As String)


Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

born2c0de
Group Icon



post 30 Jun, 2008 - 10:48 PM
Post #2
Good Job.
But just so you know, you don't need to use the OpenSubKey() method everytime.

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

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

smile.gif
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/21/08 02:17PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month