简体   繁体   English

C#中的命令行命令

[英]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需要两部分:

  1. The compiler needs to know what 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. 到类文件的顶部。 This tells the compiler to look for classes in the System.Diagnostics namespace, which is where Process lives. 这告诉编译器在System.Diagnostics名称空间( Process所在的位置)中查找类。
  2. You need to explicitly tell the compiler that you're calling a method named 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")有两个原因:

  1. 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. C#是区分大小写的语言。
  2. If you don't prepend your method call with a particular class name, the compiler looks in your own class for that method, and when it doesn't find one, it tells you about it. 如果您没有在方法调用前添加特定的类名,则编译器会在您自己的类中查找该方法,当找不到该方法时,编译器会告诉您有关该方法的信息。

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. 这种将参数作为命令行传递的方法显然不是最适合格式化的方法,但是它一直都在工作。

// Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe");

// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);

From MSDN MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM