本帖最后由 wtujoxk 于 2019-8-12 16:18 编辑
略改了一下,你自己测试看看!
[C#] 纯文本查看 复制代码 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
string command = cbx.SelectedItem.ToString() + " " + tbx4.Text.Trim();
new Thread(delegate(){ CallExe(command); }).Start();
UpdateUI(tbx4, "");
tbx4.Focus();
}
public void CallExe(object command)
{
Process p = new Process();
try
{
//command
//要执行进程的名称
p.StartInfo.FileName = System.Environment.CurrentDirectory + "\\client\\" + "homekit_download_client.exe";
p.StartInfo.Arguments = command.ToString();
// MessageBox.Show(command.ToString());
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = System.Environment.CurrentDirectory + "\\client";
//可能接受來自調用程序的輸入信息
p.StartInfo.RedirectStandardInput = true;
//由調用程序獲取輸出信息
p.StartInfo.RedirectStandardOutput = true;
//不顯示程序窗口
p.StartInfo.CreateNoWindow = true;
p.Start();//啟動程序
p.StandardInput.AutoFlush = true;
//获取CMD窗口的輸出信息:
string sOutput = p.StandardOutput.ReadToEnd();
UpdateUI(tbx2, sOutput + Environment.NewLine);
p.WaitForExit();//等待程序执行完退出进程
p.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "提示");
}
}
private void UpdateUI(Control c, string content)
{
if (c.InvokeRequired)
c.Invoke((EventHandler)delegate { c.Text = content; });
else
c.Text = content;
}
} |