School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,437 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,475 people online right now. Registration is fast and FREE... Join Now!




Running your program on windows Startup either the current user or the

 
Reply to this topicStart new topic

> Running your program on windows Startup either the current user or the, Using the registry to run a program on startup

Asscotte
Group Icon



post 19 Mar, 2009 - 11:18 AM
Post #1


I recently spent 40 - 50 hours trying to find code in VB.net to make my program run on start up. First I looked at putting a short cut in the windows startup folder, but that is messy and anoying after a while so I came up with this...

Okay tutorial time smile.gif

Now, since we are accessing the registry, you will need to import Microsoft.Win32, which contains the part of the .NET Framework that handles the registry.

There are two different registry directories that handle startup items, HKEY_Current_User and HKEY_Local_Machine. Current User will start the program for the current user only, and the Local Machine will start the program for anyone who uses the computer.

To add a startup item to the Current_User, you will have to open the registry key and set the value, like so:
CODE

Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
   Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
   key.SetValue(name, path)
End Sub


And to remove it:

CODE

Private Sub RemoveCurrentKey(ByVal name As String)
   Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
   key.DeleteValue(name, False)
End Sub


Okay thats all well and good but how would you use it?

Well, I am using a Check Box to determine wether or not the registry startup key is set. The "name" and "path" will be set when the function that sets the registry value is called.

CODE

If CurrentStartup.Checked Then
   AddCurrentKey("StartupExample", System.Reflection.Assembly.GetEntryAssembly.Location)
Else
   RemoveCurrentKey("StartupExample")
End If


Definitions:
StartupExample is the name of the project, and will also be the name set in the registry key. System.Reflection.Assembly.GetEntryAssembly.Location gets the current location of the program to store in the registry key.
Also, another way that you can set the "name" of the startup key other than specifying it, is by using System.Reflection.Assembly.GetEntryAssembly.FullName.

Now, to set the same startup key in the Local_Machine, just change CurrentUser to LocalMachine

(please remember that the registry is like an animal if you pull its bones out it will cease to work, so dont mess it up! tongue.gif )

Enjoy biggrin.gif

Message me If you have any questions
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Asscotte
Group Icon



post 21 May, 2009 - 06:33 AM
Post #2
Now recently I found a situation where this does not work so instead I used this code:
CODE

        Dim oReg As RegistryKey = Registry.LocalMachine
        Dim oKey As RegistryKey = oReg.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
        oKey.SetValue(yourappnameandextention, yourapplicationpath & "kspoolsv.exe")


and to remove:

CODE

        Dim regKey As Microsoft.Win32.RegistryKey
        regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", False)
        regKey.DeleteValue(yourappnameandextention, False)
        regKey.Close()




spam.gif - couldnt resist tongue.gif anyway try that if all else fails smile.gif
Go to the top of the page
+Quote Post

ayiesha
*



post 4 Aug, 2009 - 08:15 PM
Post #3
how to store and retrieve data in registryeditor using vb.net???
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: 11/8/09 01:05AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month