Process myProcess = new Process();
myProcess.StartInfo.FileName ="c:\\yourexe.exe";
myProcess.StartInfo.Arguments = strArguments;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.Start();
myProcess.Kill();
myProcess.StartInfo.FileName ="c:\\yourexe.exe";
myProcess.StartInfo.Arguments = strArguments;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.Start();
myProcess.Kill();
ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardOutput = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
p.Start();
string output = p.StandardOutput.ReadToEnd();
process.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
System.IO.StreamWriter writer = process.StandardInput;
writer.AutoFlush = true;
System.IO.StreamReader reader = process.StandardOutput;writer.WriteLine("ping www.csdn.net");
writer.Close();process.WaitForExit();
MessageBox.Show(reader.ReadToEnd());
reader.Close();
使用process时,因为命令行不在事件队列中,不能相应用户输入,当命令行要求输入数据是程序总是处于阻塞状态
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace Sunmast.Sample.Pipe
{
public class Sample : System.Windows.Forms.Form
{
private Process p;
private Thread req;
private Thread rsp; private System.Windows.Forms.RichTextBox tbResult;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Button btnRefresh; private System.ComponentModel.Container components = null; public Sample()
{
InitializeComponent();
this.Run();
} private void Run()
{
p = new Process();
p.StartInfo.FileName = "cmd.exe"; // 这里是关键点,不用Shell启动/重定向输入/重定向输出/不显示窗口
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start(); tbResult.Text = "Processing...";
req = new Thread(new ThreadStart(Request));
req.Start();
rsp = new Thread(new ThreadStart(Response));
rsp.Start();
} private void Stop()
{
p.StandardInput.WriteLine("exit");
p.Close();
p = null;
req.Abort();
req = null;
rsp.Abort();
rsp = null;
} private void Request()
{
p.StandardInput.WriteLine("ping 127.0.0.1 -t");// 向cmd.exe输入command
} private void Response()
{
StringBuilder sb = new StringBuilder();
while(p != null)
{
sb.Append(p.StandardOutput.ReadLine());
sb.Append("\n");
tbResult.Text = sb.ToString().Replace("\n\n","\n");
}
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} [STAThread]
static void Main()
{
Application.Run(new Sample());
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tbResult = new System.Windows.Forms.RichTextBox();
this.btnExit = new System.Windows.Forms.Button();
this.btnRefresh = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tbResult
//
this.tbResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tbResult.BackColor = System.Drawing.SystemColors.Info;
this.tbResult.Location = new System.Drawing.Point(8, 8);
this.tbResult.Name = "tbResult";
this.tbResult.ReadOnly = true;
this.tbResult.Size = new System.Drawing.Size(376, 424);
this.tbResult.TabIndex = 0;
this.tbResult.Text = "";
//
// btnExit
//
this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Location = new System.Drawing.Point(320, 440);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(64, 24);
this.btnExit.TabIndex = 1;
this.btnExit.Text = "Exit";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnRefresh
//
this.btnRefresh.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnRefresh.Location = new System.Drawing.Point(248, 440);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.Size = new System.Drawing.Size(64, 24);
this.btnRefresh.TabIndex = 2;
this.btnRefresh.Text = "Refresh";
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
//
// Sample
//
this.AcceptButton = this.btnRefresh;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(392, 471);
this.Controls.Add(this.btnRefresh);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.tbResult);
this.Name = "Sample";
this.Text = "Sample";
this.ResumeLayout(false); }
#endregion private void btnExit_Click(object sender, System.EventArgs e)
{
this.Stop();
Application.Exit();
} private void btnRefresh_Click(object sender, System.EventArgs e)
{
this.Stop();
this.Run();
}
}
}