System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo();
p.StartInfo.FileName = FilePath;
Console.WriteLine("Input the command, Please");
string strArgs = Console.ReadLine();
p.StartInfo.Arguments = strArgs;//空格分隔各個參數 這裏有兩個參數。
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;//讓窗體不顯示
p.Start();
System.IO.StreamReader reader = p.StandardOutput;//截取輸出流 ,如果exe中有Console.WriteLine("..")
string outLine = reader.ReadLine();//每次讀取一行
另外一種是調用dos,並執行命令
string strIp = ConfigurationSettings.AppSettings["IP"].ToString();
string strPath = ConfigurationSettings.AppSettings["Path"].ToString();
string strAllData = ConfigurationSettings.AppSettings["AllData"].ToString();
string strArgs = string.Empty;
string strCmd = Console.ReadLine();
if (strCmd == "show")
{
Console.WriteLine(strAllData);
}
else
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.StandardInput.WriteLine("rsh " + strIp + " " + strPath + " " + strCmd);
p.Close();
}