Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在cmd上执行两个命令_C#_Batch File - Fatal编程技术网

C# 在cmd上执行两个命令

C# 在cmd上执行两个命令,c#,batch-file,C#,Batch File,我编写了测试控制台程序。该程序使用两行命令执行cmd。但这是怎么回事? 如何编写更简单的代码,而不是编写这么大的代码 String command = @"cd c:\\test";//command get to current folder ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; start

我编写了测试控制台程序。该程序使用两行命令执行cmd。但这是怎么回事? 如何编写更简单的代码,而不是编写这么大的代码

String command = @"cd c:\\test";//command get to current folder 
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
startInfo.RedirectStandardOutput = true;
using (Process exeProcess = Process.Start(startInfo))
{
    exeProcess.WaitForExit();
}
String command = @"echo 'Hello world' > test.txt";//command write Hello world to text file
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
startInfo.RedirectStandardOutput = true;
using (Process exeProcess = Process.Start(startInfo))
{
    exeProcess.WaitForExit();
}

使用
操作符

例如:

dir & echo foo
给你:

cd c:\\test & echo 'Hello world' > test.txt

另请参见:

您可以将命令放入批处理文件
yourcmd.bat
,在您的情况下,
yourcmd.bat
将如下所示:

cd c:\test
echo "Hello world" > test.txt

然后您可以调用
System.Diagnostics.Process.Start(“yourcmd.bat”)方法,这是有效的

cd c:\test & echo "Hello world" > test.txt