Possible Duplicate:开发者_开发知识库
How to put exe file in windows Startup
Suppose I have built an application in C#, Once I install it, I want it to run in the background whenever windows starts up,or you can say a user logs in to his windows account. Is there any way I can do that? Except writing a windows service?
The application basically shows messages when a certain event is triggered Thanks
Add to shortcut to Windows start-up folder:
Environment.GetFolderPath(Environment.SpecialFolder.Startup)
Or add to registry, something like this:
RegistryKey add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
add.SetValue("Your App Name", "\"" + Application.ExecutablePath.ToString() + "\"");
You can change CurrentUser to LocalMachine if you want it to run with every user. Thanks to Aidiakapi.
This can be done using the windows registry. I recomend you to check this registry keys.
HKLM\Software\Microsoft\Windows\CurrentVersion\Run
HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKLM\Software\Microsoft\Windows\CurrentVersion\RunServices
HKLM\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce
HKCU\Software\Microsoft\Windows\CurrentVersion\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnceEx
You could add your application to the registry to run on startup at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
or
HKEY_CURREN_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
or you could add it to the startup folder for the system.
These are probably the most common/easiest options if you do not want to write a service.
You have to set up a new key in the registry pointing to your executable.
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
More information in this link http://msdn.microsoft.com/en-us/library/aa376977(v=vs.85).aspx
Easiest way is to put it or a shortcut to it in %userprofile%\Start Menu\Programs\Startupdirectory
or %allusersprofile%\Start Menu\Programs\Startup
The registry keys HKLM\Software\Microsoft\Windows\CurrentVersion\Run
(all users) and HKCU\Software\Microsoft\Windows\CurrentVersion\Run
(current user only) will also serve.
Installing it as a service is often a good approach, but not if you're going to be interactive as you say.
You can put a shortcut to the application in C:\Users\@username@\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
.
Are you using Visual Studio Setup Project? If yes you can set the shortcut directly from there.
Well this really sounds like you should use a "windows service".
There are other options like including a shortcut to the EXE into the "Startup" folder in the Programs Menu ("All Users" if you want it to run for all users on that system).
Another option would be to use the windows registry. You could add an entry to "Run" which points to the exe:
* HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
* HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
* HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
* HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
Since no-one else has mentioned it, I'll point out that you can also achieve this using a Scheduled Task who's trigger is 'At System Startup'. However, I haven't tried this with an app that needs UI interaction - it works for a background process, but I suspect it wouldn't work with something that needs a desktop context (since there isn't one until a user logs-on).
You can write the Path
to the executable in the Registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
And it will get executed every time you start windows.
精彩评论