프로그래밍을 하다보면
부팅할때 프로그램을 같이 시작해야 할 때가 있습니다
이럴때 가장 확실하고 간단한 방법은 레지스트리에 등록하는 겁니다
당연히 아무 레지스트리에나 등록한다고 되는 것은 아니고
부팅시 시작 프로그램을 등록하는 레지스트리 경로에 등록해줘야 하겠죠
이제 실제 구현 하면서 알아보도록 합시다 ㅇ_ㅇ
1. 먼저 Main Form(여기서는 AutoStartTestForm)에 버튼 두개를 등록하고 각각 Click이벤트를 만들어 줍니다

2. 아래와 같이 코드를 작성해 줍니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | namespace BlogTest { public partial class AutoStartTestForm : Form { public AutoStartTestForm() { InitializeComponent(); } private void BtnRegStart_Click( object sender, EventArgs e) { AddStartupProgram( "AutoStartTestForm" , Application.ExecutablePath); } private void BtnUnRegStart_Click( object sender, EventArgs e) { RemoveStartupProgram( "AutoStartTestForm" ); } // 부팅시 시작 프로그램을 등록하는 레지스트리 경로 private static readonly string _startupRegPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" ; private Microsoft.Win32.RegistryKey GetRegKey( string regPath, bool writable) { return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(regPath, writable); } // 부팅시 시작 프로그램 등록 public void AddStartupProgram( string programName, string executablePath) { using (var regKey = GetRegKey(_startupRegPath, true )) { try { // 키가 이미 등록돼 있지 않을때만 등록 if (regKey.GetValue(programName) == null ) regKey.SetValue(programName, executablePath); regKey.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } // 등록된 프로그램 제거 public void RemoveStartupProgram( string programName) { using (var regKey = GetRegKey(_startupRegPath, true )) { try { // 키가 이미 존재할때만 제거 if (regKey.GetValue(programName) != null ) regKey.DeleteValue(programName, false ); regKey.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } } |
3. 레지스트리에 등록 되는지 확인!


4. 컴퓨터 재시작해서 확인

코드 설명을 자세히 하려고 했습니다만...
코드도 짧고 메서드명도 직관적이라 간단한 주석만 달고 생략 했습니다
사실 레지스트리 경로만 알면 등록, 해제가 끝이거든요
혹시라도 질문 있으시면 댓글달아주세요~
'My Dev > C#' 카테고리의 다른 글
[C#, .Net] 뮤텍스(mutex)를 이용한 프로그램 중복 실행 방지 (0) | 2020.05.04 |
---|---|
[C#, .Net] 1다음에 왜 10이야? - String 정렬에 대하여 (0) | 2020.02.14 |