c#调用CMD命令行 我想写一个term,代替Windows的CMD命令行,TERM主要还是调用CMD执行指令,大概是,主窗体用两个文本框,一个用来输入指令,一个用来实时显示执行指令的输出。我看到有用process的OUTPUTDATARECEIVED时间进行监控的,再用process.BeginOutputReadLine()异步读取,但我在处理事件的委托里不能实时把读到数据传给主窗体里用来输出的文本框。请教高手怎么回事? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void ExecuteCmd(string command) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(command); p.StandardInput.WriteLine("exit"); p.WaitForExit(); this.textBox1.Text=textBox1.Text+ p.StandardOutput.ReadToEnd(); p.Close(); } C#中运行命令行截取输出流的例子说明:经常有朋友问如何在C#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在Java中实现过,由于在C#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。private void btnExecute_Click(object sender, EventArgs e) { tbResult.Text = ""; ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到 //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe start.Arguments = txtCommand.Text;//设置命令参数 start.CreateNoWindow = true;//不显示dos命令行窗口 start.RedirectStandardOutput = true;// start.RedirectStandardInput = true;// start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序 Process p=Process.Start(start); StreamReader reader = p.StandardOutput;//截取输出流 string line = reader.ReadLine();//每次读取一行 while (!reader.EndOfStream) { tbResult.AppendText(line+" "); line = reader.ReadLine(); } p.WaitForExit();//等待程序执行完退出进程 p.Close();//关闭进程 reader.Close();//关闭流 } }} 请问,如果用ping -t老死机怎么解决啊? 请问,如果用ping -t老死机怎么解决啊? 请问,如果用ping -t老死机怎么解决啊? 这个只能执行一条指令,我想把其中一个文本框中输入能在的CMD里运行的所有指令 这个只能同步显示,不能异步显示,即不能实时看到ping 指令的输出,只能等ping结束后一次性显示出来。 using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Runtime.InteropServices;using System.Threading;namespace Process{ class ReadErrorThread { Thread m_Thread; Process m_Process; String m_Error; bool m_HasExisted; object m_LockObj = new object(); public String Error { get { return m_Error; } } public bool HasExisted { get { lock (m_LockObj) { return m_HasExisted; } } set { lock (m_LockObj) { m_HasExisted = value; } } } private void ReadError() { StringBuilder strError = new StringBuilder(); while (!m_Process.HasExited) { strError.Append(m_Process.StandardError.ReadLine()); } strError.Append(m_Process.StandardError.ReadToEnd()); m_Error = strError.ToString(); HasExisted = true; } public ReadErrorThread(Process p) { HasExisted = false; m_Error = ""; m_Process = p; m_Thread = new Thread(new ThreadStart(ReadError)); m_Thread.Start(); } } class RunProcess { private String m_Error; private String m_Output; public String Error { get { return m_Error; } } public String Output { get { return m_Output; } } public bool HasError { get { return m_Error != "" && m_Error != null; } } public void Run(String fileName, String para) { StringBuilder outputStr = new StringBuilder(); try { //disable the error report dialog. //reference: http://www.devcow.com/blogs/adnrg/archive/2006/07/14/Disable-Error-Reporting-Dialog-for-your-application-with-the-registry.aspx //在注册表里禁用错误报告 Microsoft.Win32.RegistryKey key; key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"softwaremicrosoftPCHealthErrorReporting", true); int doReport = (int)key.GetValue("DoReport"); if (doReport != 0) { key.SetValue("DoReport", 0); } int showUI = (int)key.GetValue("ShowUI"); if (showUI != 0) { key.SetValue("ShowUI", 0); } } catch { } m_Error = ""; m_Output = ""; try { Process p = new Process(); p.StartInfo.FileName = fileName; p.StartInfo.Arguments = para; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); ReadErrorThread readErrorThread = new ReadErrorThread(p); while (!p.HasExited) { outputStr.Append(p.StandardOutput.ReadLine() + "rn"); } outputStr.Append(p.StandardOutput.ReadToEnd()); while (!readErrorThread.HasExisted) { Thread.Sleep(1); } m_Error = readErrorThread.Error; m_Output = outputStr.ToString(); } catch (Exception ex) { m_Error = ex.Message; } } }} 这样做,逐行读取输出流,可以解决 -t死机问题proc.Start();proc.StandardInput.WriteLine(command);proc.StandardInput.WriteLine("exit");StreamReader reader = proc.StandardOutput;string line = reader.ReadLine();while (!reader.EndOfStream){ if (line != "") { rtb.AppendText(line + "\r\n"); rtb.SelectionStart = rtb.Text.Length; rtb.ScrollToCaret(); } line = reader.ReadLine();}proc.WaitForExit();reader.Close(); 可以告诉LZ,这种方式可以实现的命令很有限,LZ可以试试ftp命令就知道了,许多输入输出是无法通过这种方式截获的。如果LZ的要求就是支持最简单的命令,那么另当别论。包括同步,实时,都不是太大的问题。另外微软上面有现成的例子,还是比较完善的。 这个代码执行-t必然死机啊,无限循环了.需要改成逐行读取,还要加个按钮停止执行,break掉. 可以在窗体中加个backgroundWorker,让代码在后台线程中执行,按下停止按钮后给WHILE循环传一个参数,让它停止.否则如果在前台执行,执行循环时按钮就都死掉不能点了. 数据库查询语句出错,求指导 CSV文件读取,日期格式问题。 C#中如何获取资源文件中的Excle文件? help~~~ 在c#.net中开发个web services在[WebMethod]中如何获取客户端的ip地址?急!! C# 调用codesoft 批量打印如何实现? 如何得到一个word文档的段落数目及如何定位到指定段的段首? EF 多表关联怎么查 请问在C#中怎么使用WINDOWS的API函数,如MessageBox这个函数????? 请教:toolbar控件能加入comboBox吗? 大家有做过XML与数据库匹配问题? C# 如何把一维数组写到xls的一列里面,不是一行。
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true; p.Start();
p.StandardInput.WriteLine(command); p.StandardInput.WriteLine("exit");
p.WaitForExit();
this.textBox1.Text=textBox1.Text+ p.StandardOutput.ReadToEnd();
p.Close();
}
说明:经常有朋友问如何在C#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在Java中实现过,由于在C#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。
private void btnExecute_Click(object sender, EventArgs e)
{
tbResult.Text = "";
ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments = txtCommand.Text;//设置命令参数
start.CreateNoWindow = true;//不显示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
Process p=Process.Start(start);
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
tbResult.AppendText(line+" ");
line = reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流
}
}
}
这个只能执行一条指令,我想把其中一个文本框中输入能在的CMD里运行的所有指令
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;namespace Process
{
class ReadErrorThread
{
Thread m_Thread;
Process m_Process;
String m_Error;
bool m_HasExisted;
object m_LockObj = new object();
public String Error
{
get { return m_Error; }
}
public bool HasExisted
{
get
{
lock (m_LockObj)
{
return m_HasExisted;
}
}
set
{
lock (m_LockObj)
{
m_HasExisted = value;
}
}
}
private void ReadError()
{
StringBuilder strError = new StringBuilder();
while (!m_Process.HasExited)
{
strError.Append(m_Process.StandardError.ReadLine());
}
strError.Append(m_Process.StandardError.ReadToEnd());
m_Error = strError.ToString();
HasExisted = true;
}
public ReadErrorThread(Process p)
{
HasExisted = false;
m_Error = "";
m_Process = p;
m_Thread = new Thread(new ThreadStart(ReadError));
m_Thread.Start();
}
}
class RunProcess
{
private String m_Error;
private String m_Output;
public String Error
{
get { return m_Error; }
}
public String Output
{
get { return m_Output; }
}
public bool HasError
{
get { return m_Error != "" && m_Error != null; }
}
public void Run(String fileName, String para)
{
StringBuilder outputStr = new StringBuilder();
try
{
//disable the error report dialog.
//reference: http://www.devcow.com/blogs/adnrg/archive/2006/07/14/Disable-Error-Reporting-Dialog-for-your-application-with-the-registry.aspx
//在注册表里禁用错误报告
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"softwaremicrosoftPCHealthErrorReporting", true);
int doReport = (int)key.GetValue("DoReport");
if (doReport != 0)
{
key.SetValue("DoReport", 0);
}
int showUI = (int)key.GetValue("ShowUI");
if (showUI != 0)
{
key.SetValue("ShowUI", 0);
}
}
catch
{
}
m_Error = "";
m_Output = "";
try
{
Process p = new Process();
p.StartInfo.FileName = fileName;
p.StartInfo.Arguments = para;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
ReadErrorThread readErrorThread = new ReadErrorThread(p);
while (!p.HasExited)
{
outputStr.Append(p.StandardOutput.ReadLine() + "rn");
}
outputStr.Append(p.StandardOutput.ReadToEnd());
while (!readErrorThread.HasExisted)
{
Thread.Sleep(1);
}
m_Error = readErrorThread.Error;
m_Output = outputStr.ToString();
}
catch (Exception ex)
{
m_Error = ex.Message;
}
}
}
}
这样做,逐行读取输出流,可以解决 -t死机问题
proc.Start();
proc.StandardInput.WriteLine(command);
proc.StandardInput.WriteLine("exit");
StreamReader reader = proc.StandardOutput;
string line = reader.ReadLine();
while (!reader.EndOfStream)
{
if (line != "")
{
rtb.AppendText(line + "\r\n");
rtb.SelectionStart = rtb.Text.Length;
rtb.ScrollToCaret();
}
line = reader.ReadLine();
}
proc.WaitForExit();
reader.Close();