命令行程序/应用程序 + C# 类方法
使用命令行程序时,通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 Process 类来执行命令行命令。
以下代码将标准输出捕获到
output
,并将进程退出代码分配给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 toexitCode
.类似于:
您通常确定 exe 的状态是否退出代码为 0,但这可以说取决于 exe 的编写者
Something like:
You usually determine an exe's state wether the exit code is 0, but that is arguably down to the writer of the exe
我假设您正在使用 Process 类来调用命令行应用程序。
您可以使用
查找进程的退出代码Process.ExitCode
。您可以通过设置ProcessStartInfo 来重定向其标准输出。在启动之前重定向StandardOutput
,然后使用Process.StandardOutput
或Process.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 settingProcessStartInfo.RedirectStandardOutput
before starting it, and then either usingProcess.StandardOutput
or theProcess.OutputDataReceived
event.看一下这个问题在此处输入链接描述。
您可能需要的附加信息是 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.为此,您可以使用
Process.Start
方法。您可以使用传入的 ProcessStartInfo 来控制进程的运行方式:For this, you use the
Process.Start
method. You can control how the process runs with the passed inProcessStartInfo
: