一。重新启动指定可执行程序。(如果程序已运行,则先中止,再运行。如果未运行,则运行它)
private void RestartApplication(string exeFileName)
{
if (string.IsNullOrEmpty(exeFileName) ||
!System.IO.File.Exists(exeFileName)) { return; }
//中止正在运行的进程。
Process[] processes = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(exeFileName));
if ((processes != null) && (processes.Length > 0))
{
processes[0].Kill();
}
//启动新进程
Process newProcess = new Process();
newProcess.StartInfo.FileName = exeFileName;
newProcess.Start();
}
二。在.net 开发RichClient应用程序中,有时要调用另外一个应用程序,并且在被调用的程序结束后,希望返回执行结果的状态。
下面的代码用appDomain 和 Process 两种技术实现这种应用:
1.用process调用一个应用程序
1.1 用ExitCode
appExePath: 应用程序
ProcessStartInfo p = new ProcessStartInfo (AppExePath);
p.WorkingDirectory = Path.GetDirectoryName(AppExePath);
//如果有参数就写在Arguments
p.Arguments = CommandLineString;
AppProcess = Process.Start (p);
//如果希望程序结束后返回结束状态
AppProcess.WaitForExit();
if (AppProcess.ExitCode == 2)
{
restartApp = true;
AppProcess = null;
StartApp_Process(true);
}
else
restartApp = false;
//AppProcess.ExitCode 返回一个int值。 被调用的程序在退出的时候可以设置Environment.ExitCode返回这个值,默认=0
//根据应用程序返回的值,可以做不同的处理。
1.2用StandardOutput标准输出
调用程序:
public static void runDBDools(string[] args)
{
Process process = new Process();
process.StartInfo.FileName = fileName;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.Arguments = string.Format("{0} {1} {2} {3} {4} {5}", args[0], args[1], args[2], DBToolsID, args[3], args[4]);
try
{
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
Application.OpenForms[0].Enabled = false;
isCall = false;
process.Start();
process.BeginOutputReadLine();//开始异步读取
while (!process.HasExited)
{
process.WaitForExit(100);
Application.DoEvents();
}
}
catch (Exception ex)
{
MessageBoxHelper.ShowError(ex.Message);
}
finally
{
Application.OpenForms[0].Enabled = true;
Application.DoEvents();
Application.OpenForms[0].Activate();
process.Close();//关闭进程
process.Dispose();//释放资源
}
}
static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (isCall) return;
try
{
Control.CheckForIllegalCrossThreadCalls = false;
if (e == null) return;
if (e.Data == null) return;
if (e.Data.Trim() == "RestoreOK")
{
Global.CloseAsk = false;
Global.NeedRelogin = true;
Application.OpenForms[0]. Close();
//Process process = new Process();
//process.StartInfo.FileName = Application.StartupPath + @"/" + "Main.exe";
//process.Start();
//isCall = true;
}
}
catch(Exception ex)
{ MessageBoxHelper.ShowError(ex.Message ); }
}
被调用程序:
Console.Write("RestoreOK");
this.Close();
2. 用AppDomain实现上述功能
AppDomain NewDomain = AppDomain.CreateDomain (
"New App Domain",
AppDomain.CurrentDomain.Evidence,
Path.GetDirectoryName(AppExePath)+@"/",
"",
false);
//Execute the app in the new appdomain
string[] cmdLineArgs;
cmdLineArgs = CommandLineArgs;
retValue = NewDomain.ExecuteAssembly(AppExePath,AppDomain.CurrentDomain.Evidence,cmdLineArgs);
//Unload the app domain
appDomain.Unload(NewDomain);
if (returnCode == 2)
{
restartApp = true;
}
else
restartApp = false;
这里retValue和应用Process返回的ExitCode一样,都是调用的程序在退出的时候的Environment.ExitCode值。
http://www.cnblogs.com/zhouto/articles/175186.html