cmd命令不在控制台中运行 [英] CMD command not running in console

查看:195
本文介绍了cmd命令不在控制台中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在C#中的cmd命令它的作品真的很好在一个WinForm但在一个控制台应用程序运行此当它不工作的一些代码。我有点卡住,为什么这是我尝试添加Windows.Forms的作为参考,并添加了使用来的代码,但这个也不能工作。我能想到的唯一的另一件事是,因为它正在作为一个控制台就不能在此之上运行另一个控制台窗口?



任何帮助表示赞赏。

  CMD的ProcessStartInfo =新的ProcessStartInfo(cmd.exe的); 
cmd.RedirectStandardInput = TRUE;
cmd.RedirectStandardOutput = TRUE;
cmd.RedirectStandardError = TRUE;
cmd.UseShellExecute = FALSE;
cmd.CreateNoWindow = TRUE;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
流程控制台=的Process.Start(CMD);
console.StandardInput.WriteLine(命令运行);


解决方案

下面的代码将执行任何你想要的控制台命令和输出在当前窗口中的控制台文本,经过一段时间(真),一切都只是作为例子:

 的ProcessStartInfo CMD =新的ProcessStartInfo( CMD.EXE); 
cmd.RedirectStandardInput = TRUE;
cmd.UseShellExecute = FALSE;
cmd.CreateNoWindow = FALSE;
cmd.WindowStyle = ProcessWindowStyle.Normal;
流程控制台=的Process.Start(CMD);

,而(真)
console.StandardInput.WriteLine(暂停);

如果您不希望任何控制台输出然后设置CreateNoWindow为true。另外这个代码控制台应用程序内使用工作System.Diagnostics程序



祝你好运!


I have some code that runs a cmd command in C# which works really well in a WinForm but when running this in a console app it doesn't work. I am a bit stuck as to why this is, I tried adding Windows.Forms as a reference and added the using to the code but this didn't work either. The only other thing I can think of is that because it is running as a console it can't run another console window on top of this?

Any help is appreciated.

ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process console = Process.Start(cmd);
console.StandardInput.WriteLine("command to run");

解决方案

The following code will perform any console command you want and output the console text in your current window, everything after while(true) is just as example:

        ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
        cmd.RedirectStandardInput = true;
        cmd.UseShellExecute = false;
        cmd.CreateNoWindow = false;
        cmd.WindowStyle = ProcessWindowStyle.Normal;
        Process console = Process.Start(cmd);

        while(true)
            console.StandardInput.WriteLine("pause");

If you don't want any console output then set CreateNoWindow to true. Also this code works inside a console application using System.Diagnostics

Good luck!

这篇关于cmd命令不在控制台中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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