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");
}
}
代码参考了之前看的一篇博客的,但是忘了是哪个了,如有博主发现,可联系我添加引用