#region
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion
namespace CSFrameworkV5.Common
{
public class ProcessStart
{
private const int WS_SHOWNORMAL = 1;
///
/// 运行外部程序,如程序已运行自动还原主窗体
///
/// 进程名称
/// 需要运行的程序文件
/// 启动程序的命令行参数
//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);
// }
//}
///
/// 查找进程
///
///
///
///
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);
}
}