以编程方式在命令提示符下运行应用程序 [英] Running an application on command prompt programmatically

查看:132
本文介绍了以编程方式在命令提示符下运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在命令提示符下自动创建一个运行应用程序(PGP)的程序。我尝试在命令提示符下手动运行该命令,一切顺利。但是,当我在代码上尝试它时,虽然没有错误,但与在命令提示符下手动键入命令时相比,结果并不相同。虽然命令是相同的。以下是我的代码:



I was trying to make a program that runs an application (PGP) on the command prompt automatically. I have tried running the command manually on the command prompt and everything went smoothly. However, when I tried it on code, though there were no errors, the results were not the same compared to when I manually typed the command on the command prompt. The commands though were the same. Below is my code:

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.UseShellExecute = false;
cmdText = @"/C pgp +batchmode C:\input.pgp\ -o C:\output.txt\";
startInfo.Arguments = cmdText;
process.StartInfo = startInfo;
process.Start();





我在命令提示符下键入的命令如下:< br $> b $ b



The command that is working when I type on the command prompt is the following:

pgp +batchmode C:\input.pgp\ -o C:\output.txt\





您之前有过类似的经历吗?我是否需要在Arguments属性上添加额外的cmd.exe开关,以使其在我在命令提示符下输入时工作?



Did you had any experience similar with this before? Are there additional switches for cmd.exe I need to add on the Arguments property to make it work like when I was typing it on the command prompt?

推荐答案

跳过CMD.EXE。



这是初学者的一个常见错误,他们不明白CMD.EXE是一个通常的程序,不会更好你的。您的程序应该是您的应用程序的父进程,而是您扮演CMD.EXE的父进程的角色,并且您希望CMD.EXE扮演其他一些子进程的父进程的角色。这是可能的,但完全是多余的。我理解它的来源:这是用户思考,非开发人员思考的结果。



所有你需要的是:

Skip that "CMD.EXE".

This is a common mistake of the beginners who don''t understand that CMD.EXE is a "usual" program, no better then yours. You program should be the parent process of your application, but instead, you play the role of the parent process of CMD.EXE, and you want CMD.EXE to play the role of the parent process for some other child process. This is possible, but totally redundant. I understand where it comes from: this is the result of user thinking, non-developer thinking.

All you need is this:
System.Diagnostics.Process.Start("pgp", @"batchmode C:\input.pgp\ -o C:\output.txt\");
// but I think trailing '\' should be removed, so it should be:
System.Diagnostics.Process.Start("pgp", @"batchmode C:\input.pgp -o C:\output.txt");





-SA


这篇关于以编程方式在命令提示符下运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆