程式碼
using System; using System.Diagnostics ; public class Sample { public static void Main() { Process pr = new Process(); pr.StartInfo.FileName = "Notepad.exe"; pr.StartInfo.Arguments = "test.dat"; pr.Start(); while (pr.HasExited == false) if ((DateTime.Now.Second % 5) == 0) { // 每五秒顯示一個tick。 Console.Write("."); System.Threading.Thread.Sleep(1000); } } }
等待應用程式執行結束,並取得應用程式輸出字串。
程式碼
using System.Diagnostics; private bool CommandLine(string args) { ProcessStartInfo proc = null ; Process p=null ; string res; try { proc = new ProcessStartInfo(); // a process holder proc.FileName = "cmd.exe"; proc.CreateNoWindow = true; proc.RedirectStandardInput = false; proc.RedirectStandardOutput = true; proc.Arguments = "/k "+args; proc.UseShellExecute = false; /*do not show console for the process - a must*/ p = Process.Start(proc); res = p.StandardOutput.ReadToEnd(); Output(res); p.WaitForExit(); /*wait indefinitely for the associated process to exit*/ return true; } catch (Exception Ex) { Output (QONSOLE + ": " + Ex.Message); return false; } finally { if (p != null) p.Close(); } }
沒有留言:
張貼留言