본문 바로가기
개발언어/C#.NET

C# Run the application at Windows startup (시작프로그램 등록하기)

by 엔돌슨 2009. 8. 8.

C#으로 시작프로그램 등록하는 방법!

참고글) http://simpcode.blogspot.com/2008/07/c-set-and-unset-auto-start-for-windows.html

원문) http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html


The Visual Studio 2005 project below includes a tiny sample application with a checkbox which you can use to definy wether or not the application should run at startup. The project can very easily be converted to older versions of Visual Studio.



Visual Studio 2005 project Download this project (Visual Studio 2005)



Making
the application run or not run at startup involves a few simple lines of code for operating on the Windows Registry. More exactly the registry key we're interested in is Run. The full path to it is HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run or if you want the setting for all the users of the operating system use HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run.



Before we start, make sure you add the following using directive:





using Microsoft.Win32;




Define a RegistryKey object we're going to use for accessing the path to the Run registry key:





// The path to the key where Windows looks for startup applications

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);





You can change CurrentUser to LocalMachine if you want the application to run at startup for all the users of the operating system.



Now here's the code used to make the current application run at startup:





// Add the value in the registry so that the application runs at startup

rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());




Of course, change MyApp to the name of your application as you want it to appear in the System Configuration Utility (msconfig.exe which can be opened by typing msconfig at Start Menu -> Run). Application.ExecutablePath.ToString()retrieves the path to the application executable that's currently running, so we don't have to write it ourselves, not to mention that we don't normally know where the user installs the application.

So basically what we did here is add a value to the Run registry key, containing the name of the application and the path to the executable. That's enough for the executable to run at startup.



Now how do we disable the application from running at startup. Obviously, we delete the registry value we just created:





// Remove the value from the registry so that the application doesn't start

rkApp.DeleteValue("MyApp", false);




And how do we check the current state of the application to see if it's set to run at startup or not.





// Check to see the current state (running at startup or not)

if (rkApp.GetValue("MyApp") == null)

{

   // The value doesn't exist, the application is not set to run at startup

   chkRun.Checked = false;

}

else

{

   // The value exists, the application is set to run at startup

   chkRun.Checked = true;

}




Where chkRun is a Checkbox which gets checked if the application is set to run at startup, and unchecked if it isn't.



There are a few other ways of making your application run at startup, such as adding it to the Startup folder of the Start menu, or adding a line to Win.ini, however using the Run registry key is the most reliable and popular method.

댓글3