让你的GUI程序随WINDOWS服务一起启动
2009-05-14 08:28:31 来源:WEB开发网3.2 检查并启动对应GUI程序
/// <summary>
/// 启动所有要启动的程序
/// </summary>
private void StartProgram()
{
try
{
foreach (string ProgramPath in rwCnfg.GsProgramPaths)
{
string fileName = "";
//fileName = System.IO.Path.GetFileName(ProgramPath); //文件名
//string ext = System.IO.Path.GetExtension(ProgramPath); //扩展名
fileName = System.IO.Path.GetFileNameWithoutExtension(ProgramPath);// fileName.Replace(ext, "");
if (!IsExistProcess(fileName))
{
ProcessStartInfo startInfo = new ProcessStartInfo(ProgramPath);
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startInfo);
Common.wLog("完成启动程序: " + fileName + ",完整路径:" + ProgramPath + " . " + System.DateTime.Now.ToString());
System.Threading.Thread.Sleep(3 * 1000); //间隔3秒;
}
}
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
/// <summary>
/// 检查该进程是否已启动
/// </summary>
/// <param name="processName"></param>
/// <returns></returns>
private bool IsExistProcess(string processName)
{
Process[] MyProcesses = Process.GetProcesses();
foreach (Process MyProcess in MyProcesses)
{
if (MyProcess.ProcessName.CompareTo(processName) == 0)
{
return true;
}
}
return false;
}
更多精彩
赞助商链接