命令行程序/应用程序 + C# 类方法

发布于 2024-11-18 10:25:13 字数 102 浏览 7 评论 0原文

使用命令行程序时,通过 ac# 类方法。

如何判断命令行程序是否成功执行以及它所执行的操作是成功还是失败?

另外,如何将屏幕命令行输出获取到 C# 类方法中?

When working with a command line program, via a c# class method.

How do you determine if the commandline program was successfully executed and the operation it has performed is ok or has failed?

Also how do you get the screen commandline output into the c# class method?

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

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

发布评论

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

评论(5

留蓝 2024-11-25 10:25:13

您可以使用 Process 类来执行命令行命令。

以下代码将标准输出捕获到 output,并将进程退出代码分配给 exitCode

using (Process p = new Process())
{
    p.StartInfo.FileName = exeName;
    p.StartInfo.Arguments = args;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    int exitCode = p.ExitCode;
}

You can use the Process class to execute a command line command.

The following code captures the standard output to output, and assigns the processes exit code to exitCode.

using (Process p = new Process())
{
    p.StartInfo.FileName = exeName;
    p.StartInfo.Arguments = args;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    int exitCode = p.ExitCode;
}
金兰素衣 2024-11-25 10:25:13

类似于:

Process mycommand = new Process();
mycommand.StartInfo.FileName = "myexe.exe";
mycommand.StartInfo.Arguments = "param1";
mycommand.StartInfo.UseShellExecute = false;
mycommand.StartInfo.RedirectStandardOutput = true;
mycommand.Start();    
Console.WriteLine(mycommand.StandardOutput.ReadToEnd());
mycommand.WaitForExit();

您通常确定 exe 的状态是否退出代码为 0,但这可以说取决于 exe 的编写者

Something like:

Process mycommand = new Process();
mycommand.StartInfo.FileName = "myexe.exe";
mycommand.StartInfo.Arguments = "param1";
mycommand.StartInfo.UseShellExecute = false;
mycommand.StartInfo.RedirectStandardOutput = true;
mycommand.Start();    
Console.WriteLine(mycommand.StandardOutput.ReadToEnd());
mycommand.WaitForExit();

You usually determine an exe's state wether the exit code is 0, but that is arguably down to the writer of the exe

漫雪独思 2024-11-25 10:25:13

我假设您正在使用 Process 类来调用命令行应用程序。

您可以使用 查找进程的退出代码Process.ExitCode。您可以通过设置 ProcessStartInfo 来重定向其标准输出。在启动之前重定向StandardOutput,然后使用 Process.StandardOutputProcess.OutputDataReceived 事件。

I assume you're using the Process class to call the command line app.

You can find the exit code of the process using Process.ExitCode. You can redirect its standard output by setting ProcessStartInfo.RedirectStandardOutput before starting it, and then either using Process.StandardOutput or the Process.OutputDataReceived event.

呆头 2024-11-25 10:25:13

看一下这个问题在此处输入链接描述

您可能需要的附加信息是 process.ExitCode 来查看它是否成功。当然,控制台应用程序的 Main 方法在不成功时必须返回退出代码,而许多应用程序则不然。

Take a look at this questionenter link description here.

The additional information you might need is process.ExitCode to see if it was sucessful. Of course, the Main method of the console app must return an exit code when it is unsuccessful, which many do not.

囚我心虐我身 2024-11-25 10:25:13

为此,您可以使用 Process.Start 方法。您可以使用传入的 ProcessStartInfo 来控制进程的运行方式:

var myProcess = Process.Start(new ProcessStartInfo {
  FileName = "process.exe",
  UseShellExecute = false,
  RedirectStandardOutput = true,
  CreateNoWindow = true
});
if (!myProcess.WaitForExit(5000)) { // give it 5 seconds to exit
  myProcess.Kill();
}
if (myProcess.ExitCode != 0) {
  // error!
}
var output = myProcess.StandardOutput.ReadToEnd(); // access output

For this, you use the Process.Start method. You can control how the process runs with the passed in ProcessStartInfo:

var myProcess = Process.Start(new ProcessStartInfo {
  FileName = "process.exe",
  UseShellExecute = false,
  RedirectStandardOutput = true,
  CreateNoWindow = true
});
if (!myProcess.WaitForExit(5000)) { // give it 5 seconds to exit
  myProcess.Kill();
}
if (myProcess.ExitCode != 0) {
  // error!
}
var output = myProcess.StandardOutput.ReadToEnd(); // access output
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文