开发者

How do I configure a C# program to run when the Operating System is first started?

开发者 https://www.devze.com 2023-01-13 10:31 出处:网络
How do I configure a C# program to run when the operating system is f开发者_StackOverflowirst started?If you want to run the program when the user logs on, then the \"Startup\" folder or Run registry

How do I configure a C# program to run when the operating system is f开发者_StackOverflowirst started?


If you want to run the program when the user logs on, then the "Startup" folder or Run registry key methods both work.

If you want the program to run when the computer is turned on (ie Windows starts), without waiting for the user to log on, you will need to install it as a service, and configure it to start automatically.


Just add the program to the Startup folder in the Start Menu.


add to registry

private void AddToRegistry()
        {
            RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            regKey.SetValue(Application.ProductName, Application.ExecutablePath);
        }


I believe you are going to have to convert this C# application into a Windows service and set its startup type to Automatic. That seems to be the approach most people use.


The easiest answer would be to add it to the Startup folder in your Start Menu. As far as I know simply dropping it in there should be enough (since it is just a little exe).


For a simple C# app, putting the app's .exe or a shortcut to it, in the start folder is the easiest approach.

To build it in a little bit more, you could add it to the registry, under "Software\Microsoft\Windows\CurrentVersion\" then the subkey that you require. For more info on the registry approach read this - http://support.microsoft.com/kb/179365

For a more complex approach which may/maynot be needed depending on your application you can create it as a service, and have it set to run automatically. For a simple app this isn't needed so I won't expand further on this point.


You can develop this program as a Windows service. Then you can configure it to re-start after a failure or subsequent failures which increases robustness. Just an idea...


I'm using Inno Setup for my installer and adding the following line will accomplish this:

Name: "{commonstartup}\YourFolder"; Filename: "{app}\YourApp.exe"; IconFilename: "{app}\YourApp.ico"

Also add this to your [setup] section

PrivilegesRequired=admin

Documentation http://www.jrsoftware.org/iskb.php?startup

0

精彩评论

暂无评论...
验证码 换一张
取 消