닷넷/C#
c# 부팅시 자동 시작하는 프로그램, 레지스트리에 등록 또는 삭제 방법
FreeBear
2021. 11. 30. 11:12
반응형
PC가 부팅이 된 후에 사용자가 어떠한 작업을 하지 않아도 바로 프로그램이 실행되게 설정하는 방법으로 이전에 쓴 글이 있는데 이 작업을 코드로 작성하기에는 어려운 부분이 있다.
2018.03.20 - [끄적이는/정보공유] - 윈도우 컴퓨터 시작시 *.exe 프로그램이 자동으로 실행되게 하는 방법
레지스트리 경로 SOFTWARE\Microsoft\Windows\CurrentVersion\Run 가 부팅 시 자동실행이 되는 프로그램들을 관리하는 경로이기 때문에 이 경로에 해당 실행파일을 등록 하거나 삭제하면 되므로 레지스트리에 등록하거나 삭제하는 방법은 코드로 작성하기 쉽다.
C#에서 레지스트리 자동실행 경로에 프로그램을 등록하는 방법은 다음과 같다.
private void SaveAutoExe()
{
// Settings에 자동실행 옵션 사용 여부를 저장한다.
Settings.Default.IsOptionAutoExecute = IsAutoExe;
Settings.Default.Save();
// 사용 여부에 따라서 레지스트리에 등록/삭제
string regPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
string programName = "프로그램이름";
try
{
using (var regKey = GetRegKey(regPath, true))
{
if (IsAutoExe)
{
// 키가 이미 등록되어 있는지 확인 후 등록
if (regKey.GetValue(programName) == null)
{
// .SetValue([레지스트리 키 이름], [프로그램 실행 경로 + 실행파일])
regKey.SetValue(programName, AppDomain.CurrentDomain.BaseDirectory + "\\" + AppDomain.CurrentDomain.FriendlyName);
}
}
else
{
// 키가 존재하는지 확인 후 삭제
if (regKey.GetValue(programName) != null)
{
regKey.DeleteValue(programName, false);
}
}
regKey.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private Microsoft.Win32.RegistryKey GetRegKey(string regPath, bool writable)
{
return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(regPath, writable);
}
반응형