C#設置點擊打開外部exe程序,並判斷是否程序已開啓,未開啓的話打開,已經在運行了就前置
[DllImport("user32.dll ")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
//根據任務欄應用程序顯示的名稱找窗口的名稱
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private const int SW_RESTORE = 9;
private void OpenSDRSharp()
{
//查找狀態中的窗口名稱來查看目標程序是否在運行運行則前置否則打開
IntPtr findPtr = FindWindow(null, "窗口名稱");
if (findPtr.ToInt32() != 0)
{
ShowWindow(findPtr, SW_RESTORE); //將窗口還原,如果不用此方法,縮小的窗口不能激活
SetForegroundWindow(findPtr);//將指定的窗口選中(激活)
}
else
{
System.Diagnostics.Process.Start(@"E:\space\***.exe");
}
}
代碼參考了之前看的一篇博客的,但是忘了是哪個了,如有博主發現,可聯繫我添加引用