tags:

views:

468

answers:

4

I want my users to be able to have my program open on startup. I am coding in vb.net. How can I go about doing this?

I can't find a "system startup" folder anywhere that I could just copy a shortcut to.

Thanks for the help!

+2  A: 

Use the registry to write the name (key) and full path (value) of your program.

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Note this will load when the user logs in, not when the system starts. You need a service as joel said to do this.

In c#.net do this :

public string GetRegistryValue(String key)
{
   return Convert.ToString(Registry.GetValue (@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", key, ""));
}

public void SetRegistryValue(String key, String value)
{
   Registry.SetValue (@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", key, value);
}

VB.net code is very similar.

Byron Whitlock
No, don't do that.
jeffamaphone
Why not?
Byron Whitlock
Perhaps you could explain why this isn't a valid approach. Are these registry keys deprecated?
Will Bickford
Any non-registry related options?
Cyclone
Why do you want to avoid the registry?
Will Bickford
Mcaffee warns me whenever a program I am trying to use edits the registry, and I do not want my program to trigger anything which might freak them out.
Cyclone
It'll work, it is just incredibly annoying and spy-ware-esque.
jeffamaphone
I totally disagree. Look at all the items in your systray and then look how many items are in your startup folder. Most applications start themselves that way. As long as you provide a way to enable/disable in the options screen, this is the normal way to go.
Byron Whitlock
+1  A: 

There are a few places where automatic startup applications are registered. In Windows XP and Vista, one such location is in a user's "Startup" folder.

C:\Documents and Settings\<username>\Start Menu\Programs\Startup

I believe this is the recommended location for applications to register themselves for startup. Most installation packagers will offer the option of registering your application for automatic startup when an .msi or other installer is built and run.

Stuart Thompson
But don't assume this exists, use the API to find it.
jeffamaphone
A very good point jeff. This path is non-stable and should be located via the Environment.GetFolderPath method you cite in your answer.
Stuart Thompson
+2  A: 

If you want your program to run on system startup, you need to build it as a service. More likely, you want it to run when the user logs in. To do that, you can configure an installer project in your solution to put an icon in the startup folder when the user clicks a radio button during install.

Joel Coehoorn
Would the windows service component suffice for this?
Cyclone
I want it to run when the user logs in, yes.
Cyclone
I think a service is overkill here.
jeffamaphone
+3  A: 

Use Environment.GetFolderPath to get the Startup folder.

jeffamaphone