从 Windows 窗体运行控制台应用程序

发布于 2024-08-01 14:36:33 字数 113 浏览 1 评论 0原文

我有一个 Windows 控制台应用程序(接受参数)并运行一个进程。 我想知道是否有任何方法可以从 Windows 窗体按钮单击事件中运行此应用程序。 我也想向它传递一个论点。

谢谢

I have a windows console app (that accepts parameters) and runs a process.
I was wondering if there was any way to run this app from within a windows form button click event. I would like to pass an argument to it as well.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说好的呢 2024-08-08 14:36:33

只需将 System.Diagnostics.Process.Start 与控制台路径一起使用应用程序,参数作为第二个参数。

Just use System.Diagnostics.Process.Start with the path to the console application, and the parameters as the second argument.

爱殇璃 2024-08-08 14:36:33

假设您有一个带有名为 txtOutput 的多行文本框的表单......

private void RunCommandLine(string commandText)
    {
        try
        {
            Process proc = new Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.Arguments = "/c " + commandText;
            txtOutput.Text += "C:\\> " + commandText + "\r\n";
            proc.Start();
            txtOutput.Text += proc.StandardOutput.ReadToEnd().Replace("\n", "\r\n");
            txtOutput.Text += proc.StandardError.ReadToEnd().Replace("\n", "\r\n");
            proc.WaitForExit();
            txtOutput.Refresh();
        }
        catch (Exception ex)
        {
            txtOutput.Text = ex.Message;
        }
    }

Assuming you have a form with a multiline textbox called txtOutput.....

private void RunCommandLine(string commandText)
    {
        try
        {
            Process proc = new Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.Arguments = "/c " + commandText;
            txtOutput.Text += "C:\\> " + commandText + "\r\n";
            proc.Start();
            txtOutput.Text += proc.StandardOutput.ReadToEnd().Replace("\n", "\r\n");
            txtOutput.Text += proc.StandardError.ReadToEnd().Replace("\n", "\r\n");
            proc.WaitForExit();
            txtOutput.Refresh();
        }
        catch (Exception ex)
        {
            txtOutput.Text = ex.Message;
        }
    }
缺⑴份安定 2024-08-08 14:36:33

您需要使用 System.Diagnostics.Process

You'll want to use System.Diagnostics.Process

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文