1. 程式人生 > >wpf 開啟外部程式並在需要時啟用

wpf 開啟外部程式並在需要時啟用

開啟外部程式使用Process

啟用窗體,通過SetForegroundWindow函式傳遞Process獲取的控制代碼啟用

_external = new Process();
[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern int SetForegroundWindow(IntPtr hWnd);

public void Run(string file, string args)
        {
            bool _bExisted = true;


            try
            {
                if (_external.HasExited == false)
                {
                    _bExisted = false;
                    SetForegroundWindow(_external.MainWindowHandle);
                    return;
                }
            }
            catch (Exception e)
            {
                Global.MainLogging.LogError("ExternalProcess.Run:param1:" + file + ",param2:" + args + "," + e.ToString());
            }
            finally
            {
                if (_bExisted) RunExternal(file, args);
            }
        }