程序的目的是将 Run.cmd 运行时的内容输出到listbox1中。但是用以下的代码老是出错,说是在”bool b = p.Start();“时找不到文件, Run.cmd 是自己定义的一个文件,已经放在相应的目录下了。哪位大侠可以指导一下?private void btn_Click(object sender, EventArgs e)
{ ParameterizedThreadStart ts = new ParameterizedThreadStart(TCwork);
Thread th = new Thread(ts);
th.Start(this);
} public static void TCwork(object x)
{
frmMain dlg = (frmMain)x; // 获取窗体指针
Process p = new Process(); // 创建一个cmd进程
p.StartInfo.CreateNoWindow = true; // 无窗口
p.StartInfo.WorkingDirectory = "G:\\“;
p.StartInfo.FileName = "Run.cmd"// 运行的程序
p.StartInfo.Arguments = "169.2.2.2"; // 传入参数
p.StartInfo.UseShellExecute = false; // 不用外壳程序启动
p.StartInfo.RedirectStandardOutput = true; // 重定向输出流,这条和上一条语句可以获取输出流
bool b = p.Start();
StreamReader sr = p.StandardOutput; // 获取输出流
while (sr.EndOfStream != true)
{
string txt = sr.ReadLine();
dlg.SetText(txt);
} } public delegate void SetTextHandler(string text); private void SetText(string text)
{ if (lbTestStatus.InvokeRequired==true)
{ SetTextHandler set = new SetTextHandler(SetText);//委托的方法参数应和SetText一致 lbTestStatus.Invoke(set, new object[] { text }); //此方法第二参数用于传入方法,代替形参text
return;
} else
{
lbTestStatus.Items.Add(text); } }
{ ParameterizedThreadStart ts = new ParameterizedThreadStart(TCwork);
Thread th = new Thread(ts);
th.Start(this);
} public static void TCwork(object x)
{
frmMain dlg = (frmMain)x; // 获取窗体指针
Process p = new Process(); // 创建一个cmd进程
p.StartInfo.CreateNoWindow = true; // 无窗口
p.StartInfo.WorkingDirectory = "G:\\“;
p.StartInfo.FileName = "Run.cmd"// 运行的程序
p.StartInfo.Arguments = "169.2.2.2"; // 传入参数
p.StartInfo.UseShellExecute = false; // 不用外壳程序启动
p.StartInfo.RedirectStandardOutput = true; // 重定向输出流,这条和上一条语句可以获取输出流
bool b = p.Start();
StreamReader sr = p.StandardOutput; // 获取输出流
while (sr.EndOfStream != true)
{
string txt = sr.ReadLine();
dlg.SetText(txt);
} } public delegate void SetTextHandler(string text); private void SetText(string text)
{ if (lbTestStatus.InvokeRequired==true)
{ SetTextHandler set = new SetTextHandler(SetText);//委托的方法参数应和SetText一致 lbTestStatus.Invoke(set, new object[] { text }); //此方法第二参数用于传入方法,代替形参text
return;
} else
{
lbTestStatus.Items.Add(text); } }
把路径带上不行吗?
或者把Run.cmd拷贝到你的程序目录下