1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#region
 
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    public class ProcessStart
    {
        private const int WS_SHOWNORMAL = 1;
 
        /// <summary>
        /// 运行外部程序,如程序已运行自动还原主窗体
        /// </summary>
        /// <param name="processName">进程名称</param>
        /// <param name="filePath">需要运行的程序文件</param>
        /// <param name="args">启动程序的命令行参数</param>
        //public static void RunProgram(string processName, string filePath, string args)
        //{
        //    try
        //    {
        //        //查找进程
        //        Process por = FindProcess(processName, filePath);
        //        if (por != null)
        //        {
        //            //如程序已运行自动还原主窗体
        //            ShowWindowAsync(por.MainWindowHandle, WS_SHOWNORMAL);
        //            SetForegroundWindow(por.MainWindowHandle);
        //            return;
        //        }
 
        //        //启动进程
        //        ProcessStartInfo p = new ProcessStartInfo();
        //        p.FileName = filePath;
        //        p.WindowStyle = ProcessWindowStyle.Normal;
        //        p.Arguments = args;
 
        //        Process pr = Process.Start(p);
 
        //        Application.DoEvents();
        //        Thread.Sleep(2000);//等待启动程序
        //    }
        //    catch (Exception ex)
        //    {
        //        Msg.ShowException(ex);
        //    }
        //}
 
        /// <summary>
        ///     查找进程
        /// </summary>
        /// <param name="processName"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Process FindProcess(string processName, string filePath)
        {
            var ps = Process.GetProcessesByName(processName);
            foreach (var por in ps)
                if (por.MainModule.FileVersionInfo.FileName.ToUpper() ==
                    filePath.ToUpper())
                    return por;
 
            return null;
        }
 
        [DllImport("User32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
 
        [DllImport("User32.dll")]
        public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
    }
}