c# winform调用cmd运行一控制台程序输出异常

hinstriven 2014-06-10 01:24:09
使用winform调用cmd运行一控制台程序MSO.exe,将运行结果重定向输出到winform一个textbook里。MSO.exe带三个参数,执行时为MSO.exe 参数1 参数2 参数3,程序运行中间本来应该会输出"Input serial number again"提示用户再次输入一个参数,但是我的winform显示cmd输出内容的textbook没输出这一行"Input serial number again",也就是说到这里我无法再从winform去输入命令行让MSO.exe继续执行下去了!请高手指点,谢谢了!
...全文
470 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tcmakebest 2014-06-10
  • 打赏
  • 举报
回复
在一行没有结束的时候,不会有输出,所以请直接输入要输入的内容:US12345678 + 回车,之后就会一起显示。
hinstriven 2014-06-10
  • 打赏
  • 举报
回复

正常情况下在dos运行应该是上面这个界面,输入“secureDataTool.exe USB0::2391::6056::US12345678::INSTR service snumber US12345678 mnumber MSOX3000A”后dos会输出一些内容,然后提示“Enter the serial numberr again to continue”,接着再次输入US12345678。

现在情况是我做了个winform来做这个secureDataTool.exe的外壳,程序运行后只显示到这里

我的后台代码如下,麻烦高手指点迷津,感激不尽啊,谢谢!
namespace secureDataToolGUI
{
public partial class Form1 : Form
{

Process p;
StreamWriter input;

public Form1()
{
InitializeComponent();

p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;

p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

p.Start();
input = p.StandardInput;
p.BeginOutputReadLine();
}

void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
update(e.Data + Environment.NewLine);
}

private void button1_Click(object sender, EventArgs e)
{
input.WriteLine(textBox1.Text);
if (textBox1.Text.Contains("\\"))
{
textBox1.Text = "secureDataTool.exe " + label1.Text + textBox3.Text + label2.Text + textBox4.Text + label3.Text + textBox5.Text + label4.Text + textBox6.Text + label5.Text + textBox7.Text;
button1.Text = "RUN";
}
else
{
textBox1.Text = "";
button1.Text = "Load Program";
}
}

//申明delegate更新数据
delegate void updateDelegate(string msg);
void update(string msg)
{
if (this.InvokeRequired)
Invoke(new updateDelegate(update), new object[] { msg });
else
{
textBox2.Text += msg;
textBox2.SelectionStart = textBox2.Text.Length - 1;
textBox2.ScrollToCaret();
}
}
//关闭GUI窗口
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
p.Close();
}
//打开文件浏览对话框
private void button2_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
}
//获取文件路径,将路径定位到文件所在位置
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
string filepath = openFileDialog1.FileName;
int fileposition = filepath.LastIndexOf("\\");
textBox1.Text = "cd " + filepath.Substring(0, fileposition);
}
#region
//private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
//{
// if (!Char.IsNumber(e.KeyChar) && Convert.ToInt32(e.KeyChar) != 8)
// {
// e.Handled = true;
// }
//}

//private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
//{
// if (!Char.IsNumber(e.KeyChar) && Convert.ToInt32(e.KeyChar) != 8)
// {
// e.Handled = true;
// }
//}

//private void textBox5_Leave(object sender, EventArgs e)
//{
// System.Text.RegularExpressions.Regex myregex = new System.Text.RegularExpressions.Regex("[A-Z]{2}[0-9]{8}");
// if (!myregex.IsMatch(textBox5.Text))
// {
// MessageBox.Show("输入格式有误!");
// textBox5.Text = "";
// }
//}

//private void textBox6_Leave(object sender, EventArgs e)
//{
// System.Text.RegularExpressions.Regex myregex = new System.Text.RegularExpressions.Regex("[A-Z]{2}[0-9]{8}");
// if (!myregex.IsMatch(textBox6.Text))
// {
// MessageBox.Show("输入格式有误!");
// textBox6.Text = "";
// }
//}
#endregion
}
}
hinstriven 2014-06-10
  • 打赏
  • 举报
回复
问题解决了,就是照着你说的那样做,谢谢了! 我想加个功能,就是在最后要执行“US12345678 + 回车”前能否先弹出一个messagebox提示用户呢,我对于线程这块不是很熟悉,谢谢了!
tcmakebest 2014-06-10
  • 打赏
  • 举报
回复
既然把输出重定向了,你确保已经正确处理了吗

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧