C#을 사용하여 Windows 시작 시 자동으로 백그라운드에서 실행되는 코드
이를 위해 Windows 레지스트리를 사용하여 애플리케이션을 시작 프로그램에 등록할 것입니다.
csharp
Copy code
using Microsoft.Win32;
using System;
using System.Reflection;
class Program
{
static void Main()
{
// 실행 파일 경로 가져오기
string exePath = Assembly.GetExecutingAssembly().Location;
// 시작 프로그램 레지스트리 키 경로
string runKeyPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
try
{
// 시작 프로그램 레지스트리 키 열기
RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(runKeyPath, true);
// 애플리케이션을 시작 프로그램에 등록
startupKey.SetValue("MyApplication", exePath);
startupKey.Close();
Console.WriteLine("애플리케이션이 시작 프로그램에 등록되었습니다.");
}
catch (Exception ex)
{
Console.WriteLine("시작 프로그램 등록 중 오류가 발생했습니다: " + ex.Message);
}
// 다음 코드를 작성하여 애플리케이션이 백그라운드에서 계속 실행되도록 유지합니다.
while (true)
{
// 원하는 작업을 수행하세요.
}
}
}
이 코드는 현재 실행 중인 애플리케이션의 실행 파일 경로를 가져와서 해당 경로를 Windows 시작 프로그램 레지스트리에 등록하는 방법을 보여줍니다. 실행 파일은 "MyApplication"이라는 이름으로 등록됩니다. 레지스트리 키를 열고 등록한 후에는 애플리케이션이 백그라운드에서 계속 실행되도록 유지됩니다. while 루프 안에 필요한 작업을 추가하여 원하는 작업을 수행하실 수 있습니다.
참고: 이 코드는 현재 사용자의 시작 프로그램에 애플리케이션을 등록합니다. 따라서 코드를 다른 사용자로 실행할 경우 해당 사용자의 시작 프로그램에 등록됩니다.
I am a father of one boy and two girls living in Cheongju. We love to go "plogging" together every day. Plogging is an activity where we pick up litter while taking a walk, with the goal of preserving the environment and creating a clean community. It's a valuable time for our family to join forces.
You can find our activities on YouTube at https://www.youtube.com/@0070TV. We kindly ask for your support and encouragement.
'[프로그램]' 카테고리의 다른 글
| c# 논리 연산 코드 8가지 NOT and or exor if 삼항연산 null (0) | 2023.06.27 |
|---|---|
| c# txt to txt통신 코드 (1) | 2023.06.27 |
| C# 포커 게임의 예시 소스 코드 (0) | 2023.06.26 |
| C# PDI온도 제어 소스 (1) | 2023.06.26 |
| C# 윈도우 화면비율 자동채움 프레임워크 코드 무료소스 오픈소스 (0) | 2023.06.26 |