[英]Command Line Commands in C#
I want to run a few Windows programs from C#. 我想从C#运行一些Windows程序。 How would I do this?
我该怎么做? From what I've been looking up, it has something to do with System.Diagnostics.Process 's start method
根据我一直在寻找的东西,它与System.Diagnostics.Process的start方法有关。
Would I just do using 我会用吗
System.Diagnostics.Process;
and then type 然后输入
start("Command to be executed");
or am I looking at this problem incorrectly? 还是我看错了这个问题?
New to C#. C#的新手。
Based on your comments it seems that you are unfamiliar with
object-oriented programming
namespaces and classes. 根据您的评论,您似乎不熟悉
面向对象的编程
名称空间和类。 Let's break it down. 让我们分解一下。
Process
is a class, part of the .NET framework. Process
是一类,是.NET框架的一部分。 Process
has has a collection of methods, some of them being static
methods. Process
具有方法的集合,其中一些是static
方法。 Start
is one of those static methods. Start
是这些静态方法之一。 There are two pieces necessary in order for you to use Process.Start
: 为了使您能够使用
Process.Start
需要两部分:
Process
is. Process
。 You can give that info to the compiler by adding using System.Diagnostics;
using System.Diagnostics;
添加该信息给编译器using System.Diagnostics;
to the top of your class file. System.Diagnostics
namespace, which is where Process
lives. System.Diagnostics
名称空间( Process
所在的位置)中查找类。 Start
that is part of the process class . Start
的方法,该方法是流程类的一部分 。 You can do this by using Process.Start()
. Process.Start()
来完成此操作。 Or in your case, Process.Start("Command to be executed");
Process.Start("Command to be executed");
There are two reasons why you CANNOT just type start("Command to be executed")
: 为什么不能只键入
start("Command to be executed")
有两个原因:
start
, with a lowercase "s", is not the same as Start
with an uppercase "S". start
,用小写字母“S”,是不一样的Start
用大写字母“S”。 C# is a case-sensitive language. The thing is: essentially the "Command to be executed" part is what you would type in the command prompt. 问题是:本质上,“要执行的命令”部分就是您在命令提示符下键入的内容。 For example:
例如:
Process.Start("C:\Programs\programFile.exe",
"/arg1='This is an argument' -arg2=anotherArgument someOtherArgument");
The entry point of your program (which file is located at "C:\\Programs\\programFile.exe") would receive the following argument list in its main method: 程序的入口点(该文件位于“ C:\\ Programs \\ programFile.exe”)在其主要方法中将收到以下参数列表:
args[0] = "/arg1='This is an argument'"
args[1] = "-arg2=anotherArgument"
args[2] = "someOtherArgument"
This way of passing parameters as a command line is obviously not the greatest to format, but it does the job all the time. 这种将参数作为命令行传递的方法显然不是最适合格式化的方法,但是它一直都在工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.