有時在進行整合的時候, 須要啟動本身以外的程式並等待其執行結果. 再視結果決定行動. 在.net Framework 中, 可以利用ProcessInfo 實現.
private static string RunExternalProgram(string a_filename, string[] a_arguments, string a_workdirectory)
{
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
Info.FileName = a_filename;
Info.Arguments = string.Join(" ", a_arguments);
Info.WorkingDirectory = a_workdirectory;
Info.RedirectStandardError = true;
Info.UseShellExecute = false;
System.Diagnostics.Process Proc;
String errorMsg = string.Empty;
try
{
Proc = System.Diagnostics.Process.Start(Info);
errorMsg = Proc.StandardError.ReadToEnd();
Proc.WaitForExit();
return errorMsg;
}
catch (System.ComponentModel.Win32Exception ex)
{
throw ex;
}
}
Leave a Reply