C#启动一个EXE,直到完全启动,然后附加参数

发布于 2025-02-11 10:20:02 字数 1622 浏览 2 评论 0原文

我试图通过process.start开始使用参数。 我的第一个尝试是使用process.start(“/of/of/of/of/the/exe”,“ exe的参数”)。 这是我的代码片段:

Process.Start(@"D:\Program Files\ITASCA\UDEC700\Exe64\udecConsole2017.exe", @"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

但是,此EXE的初始化有点慢,结果是,我只能启动EXE,而是通过参数失败。以下是屏幕截图: 这与没有参数开始的结果完全相同。

通过引用此帖子 c# - 制作过程。开始等待该过程具有启动,我将代码更改为以下:

var process = Process.Start(@"D:\Program Files\ITASCA\UDEC700\Exe64\udecConsole2017.exe", @"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

while (string.IsNullOrEmpty(process.MainWindowTitle))
{
       System.Threading.Thread.Sleep(100);
       process.Refresh();
}

但是这些更改不起作用。

我认为我的目标是等到EXE完全启动,然后用参数运行它,但我不知道该如何实施。

=============================================== ===

新加法: 如果我输入参数调用'd:\ work \ 202205 \ 20220525 \ tunnel-for-cmd.txt'在此开始过程中,我将得到我的结果: 所以我认为输入论点应该可以吗?

====================================== 新添加2:

检查输出端的代码

I was trying to start a exe with arguments by Process.Start.
My first try is using Process.Start("Path/of/the/exe", "arguments of exe").
Here's my code snippets:

Process.Start(@"D:\Program Files\ITASCA\UDEC700\Exe64\udecConsole2017.exe", @"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

However the initialization of this exe is a bit slow, and the result is, I can only start the exe but the failed passing arguments. The following is the screenshot:
enter image description here
which is exactly the same result that starts without arguments.

By referencing this post C# - Making a Process.Start wait until the process has start-up, I changed my code as follows:

var process = Process.Start(@"D:\Program Files\ITASCA\UDEC700\Exe64\udecConsole2017.exe", @"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

while (string.IsNullOrEmpty(process.MainWindowTitle))
{
       System.Threading.Thread.Sleep(100);
       process.Refresh();
}

however these changes does not work.

I think my goal is to wait until exe completely started and then run it with arguments, but I dont know how to implement this.

=====================================================

New additions:
if I type in arguments call 'D:\Work\202205\20220525\tunnel-for-cmd.txt' in this started process, I will get my result:
enter image description here
SO I think the input arguments should be OK?

=======================================
new addition 2:

code for checking outputstream end
enter image description here

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

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

发布评论

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

评论(1

执手闯天涯 2025-02-18 10:20:02

看来这是一个控制台应用程序,您将在控制台启动后输入。该键入不是参数:仅在启动新过程而永不更改时提供参数。

您正在做的是为程序的标准输入提供一些东西。控制台程序具有OS提供的三个流(一个输入和两个输出)。您需要重定向它们以检测程序何时启动并提供适当的输入。

这样的事情:

// Start with stdio redirected
var psi = new ProcessStartInfo()
{
    UseShellExecute = false,
    FileName = @"your exe",
    RedirectStandardInput = true, 
    RedirectStandardOutput = true,
};
var p = System.Diagnostics.Process.Start(psi);

// Read until the udec> prompt
while(true)
{
    var line = p.StandardOutput.ReadLine();
    if(line.StartsWith("udec>"))
        break;
}

// Write the command
p.StandardInput.WriteLine(@"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

// Read the result
p.StandardOutput.ReadToEnd();

It appears this is a console application and you are typing in the console after it starts. This typing is not arguments: Arguments are provided only when starting a new process and never change.

What you are doing is providing something to the standard input of the program. Console programs have three streams the OS provides (one input and two output). You need to redirect these to detect when the program has started and to provide the proper input.

Something like this:

// Start with stdio redirected
var psi = new ProcessStartInfo()
{
    UseShellExecute = false,
    FileName = @"your exe",
    RedirectStandardInput = true, 
    RedirectStandardOutput = true,
};
var p = System.Diagnostics.Process.Start(psi);

// Read until the udec> prompt
while(true)
{
    var line = p.StandardOutput.ReadLine();
    if(line.StartsWith("udec>"))
        break;
}

// Write the command
p.StandardInput.WriteLine(@"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

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