如何在 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
How to execute command in a C# Windows console app?
提问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
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# 支持四种不同的循环结构:
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.
文件相关的操作可以分别使用File和Directory类来执行。例如,要重命名文件,您可以像这样使用 File 类的 Move 方法。
File.Move("oldName","NewName");
File.Move("oldName","NewName");
Because both oldName
and NewName
are assumed to be in the same directory, the file named oldName
is renamed to NewName
.
因为假定oldName
和NewName
都在同一目录中,所以命名的文件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.File
class with the Move
method
如果您所做的只是重命名文件,则可以通过将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