닷넷/WinForms

C# 프로그램 실행 중복방지

FreeBear 2017. 4. 26. 17:24
반응형

Program.cs 파일에

int thisID = System.Diagnostics.Process.GetCurrentProcess().Id;  // 현재 기동한 프로그램 id
 
// 실행 중인 프로세스 중에서 현재 기동한 프로그램과 같은 이름을 가진 프로그램 수집
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("Process Name");

if (p.Length > 1)
{
    for (int i = 0; i < p.Length; i++)
    {
          if (p[i].Id == thisID)
            {
                 MessageBox.Show("이미 실행 중인 프로그램입니다.",
                                 "확인",
                                  MessageBoxButtons.OK,
                                  MessageBoxIcon.Warning,
                                  MessageBoxDefaultButton.Button1);
                        p[i].Kill();
                        break;
               }
       }
}

// Application 실행
cs


반응형