在 C# 中将参数传递给正在运行的进程

发布于 2024-11-25 06:39:42 字数 310 浏览 1 评论 0原文

我在运行进程并将参数传递给它们时遇到一些麻烦。 我知道如何使用一些参数运行进程

ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c something");  
Process p = Process.Start(psi)

问题是执行脚本后进程被终止。这就是为什么有“/c”,

但我正在运行多个脚本,我想在一个进程(“cmd.exe”)中运行它们,而不是每次都启动新进程。

有一些解决方案吗?

我希望有人能理解我在说什么;)

I've some troubles with running processes and passing args to them.
I know how to run process with some args

ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c something");  
Process p = Process.Start(psi)

The problem is that after script is executed process is terminated. That's why there is "/c"

But I'm running multiple scripts and I would like to run them in one process ("cmd.exe") not to start new process every time.

Is there some solutions for it ?

I hope somebody understand what I'm talking about ;)

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

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

发布评论

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

评论(2

池木 2024-12-02 06:39:42

我建议您使用批处理文件来编写可执行文件的执行脚本并调用批处理文件。或者,你可以这样做 -

    Process p = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = "cmd.exe";
    info.RedirectStandardInput = true;
    info.UseShellExecute = false;

    p.StartInfo = info;
    p.Start();

    using (StreamWriter sw = new StreamWriter(p.StandardInput))
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine("mysql -u root -p");
            sw.WriteLine("mypassword");
            sw.WriteLine("use mydb;");
        }
    }

I recommend you utilize a batch file to script the execution of your executables and call your batch file instead. Or, you can do this -

    Process p = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = "cmd.exe";
    info.RedirectStandardInput = true;
    info.UseShellExecute = false;

    p.StartInfo = info;
    p.Start();

    using (StreamWriter sw = new StreamWriter(p.StandardInput))
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine("mysql -u root -p");
            sw.WriteLine("mypassword");
            sw.WriteLine("use mydb;");
        }
    }
简单 2024-12-02 06:39:42

听起来您应该研究重定向标准输入 - 请务必将 psi.UseShellExecute 设置为 false。您可能还想重定向标准输出,这样您就可以通过某种方式了解您的子进程正在做什么。

此处。

It sounds like you ought to investigate redirecting the standard input - be sure to also set psi.UseShellExecute to false. You'll probably also want to redirect standard output, so you can have some way of knowing what your child process is doing.

Read more about redirection here.

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