启动命令窗口并运行命令里面 [英] Start command windows and run commands inside

查看:205
本文介绍了启动命令窗口并运行命令里面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始与一些参数的命令窗口并运行里面的命令。



例如,启动一个TEST.CMD和运行的mkdir。



我可以的ProcessStartInfo启动TEST.CMD,但我不知道怎么办好进一步的命令。我可以通过进一步的参数给TEST.CMD过程?



我怎么去呢?



无法添加注释来回答......所以写在这里。



安德烈,这就是我一直在寻找。但是对我来说,上面的代码does not工作。



我推出一个TEST.CMD这是新的命令环境(如拉扎勒构建环境),我需要运行更多的命令。

  psi.FileName = @C:\test.cmd 
psi.Arguments = @为arg0 ARG1 ARG2

psi.RedirectStandardInput = TRUE;
psi.RedirectStandardOutput = TRUE;
psi.CreateNoWindow = TRUE;
psi.UseShellExecute = FALSE;

进程p =新工艺();
p.StartInfo =磅;
p.Start();
p.StandardInput.WriteLine(@DIR> C:\results.txt);
p.StandardInput.WriteLine(@DIR> C:\results2.txt);


解决方案

您可以发送进一步的命令使用cmd.exe的到过程
标准输入。你必须重定向它,是这样的:

  VAR的StartInfo =新的ProcessStartInfo 
{
的FileName = CMD.EXE,
RedirectStandardInput = TRUE,
RedirectStandardOutput = TRUE,
UseShellExecute =假,
CreateNoWindow = TRUE
};

VAR过程=新的Process {StartInfo的=的StartInfo};

的Process.Start();
process.StandardInput.WriteLine(@DIR> C:\results.txt);
process.StandardInput.WriteLine(@DIR> C:\results2.txt);
process.StandardInput.WriteLine(退出);

process.WaitForExit();



记得写退出作为您的最后一个命令,否则CMD过程不正确终止。 ..


I need to start the command window with some arguments and run more commands inside.

For example, launch a test.cmd and run mkdir.

I can launch the test.cmd with processstartinfo , but i am not sure how to run further commands. Can I pass further arguments to the test.cmd process?

How do I go about this?

Unable to add comments to answer... SO writing here.

Andrea, This is what I was looking for. However the above code doesnt work for me.

I am launching a test.cmd which is new command environment (like razzle build environment) and I need to run further commands.

psi.FileName = @"c:\test.cmd";
psi.Arguments = @"arg0 arg1 arg2";

psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;

Process p = new Process();
p.StartInfo = psi;
p.Start();
p.StandardInput.WriteLine(@"dir>c:\results.txt");
p.StandardInput.WriteLine(@"dir>c:\results2.txt"); 

解决方案

You can send further commands to cmd.exe using the process standard input. You have to redirect it, in this way:

var startInfo = new ProcessStartInfo
                    {
                        FileName = "cmd.exe",
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    };

var process = new Process {StartInfo = startInfo};

process.Start();
process.StandardInput.WriteLine(@"dir>c:\results.txt");
process.StandardInput.WriteLine(@"dir>c:\results2.txt");
process.StandardInput.WriteLine("exit");

process.WaitForExit();

Remember to write "exit" as your last command, otherwise the cmd process doesn't terminate correctly...

这篇关于启动命令窗口并运行命令里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆