如何在 C# Windows 控制台应用程序中执行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6798547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:33:24  来源:igfitidea点击:

How to execute command in a C# Windows console app?

c#.netwindowsconsole-application

提问by user776676

How to implement this pseudo code in a C# Windows console app?

如何在 C# Windows 控制台应用程序中实现此伪代码?

for i=1 to 100
    rename filei newFilei

The key goal is to loop and execute any cmd command within a Windows console app.

关键目标是在 Windows 控制台应用程序中循环并执行任何 cmd 命令。

class Program
{
    static void Main(string[] args)
    {
        string strCmdLine;
        System.Diagnostics.Process process1;
        process1 = new System.Diagnostics.Process();


        Int16 n = Convert.ToInt16(args[1]);
        int i;
        for (i = 1; i < n; i++)
        {
            strCmdLine = "/C xxxxx xxxx" + args[0] + i.ToString();
            System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
            process1.Close();
        }


    }
}

回答by CharithJ

Diagnostics.Process

诊断程序

            System.Diagnostics.Process proc;

            cmd = @"strCmdLine = "/C xxxxx xxxx" + args[0] + i.ToString();

            proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "cmd";
            proc.StartInfo.Arguments = cmd;
            proc.Start();

回答by Crippledsmurf

C# supports four different loop constructs:

C# 支持四种不同的循环结构:

  1. for
  2. foreach
  3. while
  4. do
  1. 为了
  2. foreach
  3. 尽管

The documentation for each of these is sufficiently detailed that I won't explain these again here.

每一个的文档都足够详细,我不会在这里再次解释这些。

File related operations can be performed with the Fileand Directoryclasses respectively. For example to rename a file you could use the Move method of the File class like so.

文件相关的操作可以分别使用FileDirectory类来执行。例如,要重命名文件,您可以像这样使用 File 类的 Move 方法。

File.Move("oldName","NewName");

File.Move("oldName","NewName");

Because both oldNameand NewNameare assumed to be in the same directory, the file named oldNameis renamed to NewName.

因为假定oldNameNewName都在同一目录中,所以命名的文件oldName被重命名为NewName.

With respect to launching other applications the Processclass offers the ability to launch a process and monitor it's execution. I'll leave investigating this class and it's capabilities to the reader.

对于启动其他应用程序,Process类提供启动进程并监视其执行的能力。我将把调查这个类和它的功能留给读者。

The pseudo-code included in the question could be translated to the following code in C#. Please note that this sample does not include any error handling which one will always want to include in production code.

问题中包含的伪代码可以在 C# 中转换为以下代码。请注意,此示例不包含任何人们总是希望包含在生产代码中的错误处理。

string[] sourceFileNames=new string[100];
string[] destFileNames = new string[sourceFileNames.Length];
//fill arrays with file names.
for (int i=0; i < fileNames.Length; i++)
{
File.Move(sourceFileNames[i], destFileNames[i]);
}

回答by Antony Scott

If all you're doing is renaming files you can do that by using the System.IO.Fileclass with the Movemethod

如果您所做的只是重命名文件,则可以通过将System.IO.File类与Move方法一起使用来实现

http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx

http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx