C#判断某程序是否运行的方法
程序员文章站
2023-12-16 16:50:22
本文实例讲述了c#判断某程序是否运行的方法,分享给大家供大家参考。
具体实现方法如下:
[dllimport("user32.dll")]
private s...
本文实例讲述了c#判断某程序是否运行的方法,分享给大家供大家参考。
具体实现方法如下:
[dllimport("user32.dll")] private static extern bool setforegroundwindow(intptr hwnd); [dllimport("user32.dll")] private static extern bool showwindowasync(intptr hwnd, int ncmdshow); [dllimport("user32.dll")] private static extern bool isiconic(intptr hwnd); // 消息函数 [dllimport("user32.dll", entrypoint = "postmessagea")] public static extern bool postmessage(intptr hwnd, int msg, int wparam, int lparam); [dllimport("user32.dll")] public static extern intptr findwindow(string strclassname, string strwindowname); [dllimportattribute("user32.dll")] public static extern int sendmessage(intptr hwnd, int msg, int wparam, int lparam); public const int wm_syscommand = 0x0112; public const int sc_maximize = 0xf030; private string exename = "saomiaoapp"; public void setform() { process[] processes = process.getprocessesbyname(exename); if (processes.length > 0) { intptr hwnd = processes[0].mainwindowhandle; if (isiconic(hwnd)) showwindowasync(hwnd, 9);// 9就是sw_restore标志,表示还原窗体 //sendmessage(hwnd, wm_syscommand, sc_maximize, 0); setforegroundwindow(hwnd); } else { process.start(exename + ".exe"); } }
希望本文所述对大家的c#程序设计有所帮助。