一、

using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write(args[]);
}
}
}

编译生成ConsoleApp1.exe,并放到ConsoleApp2-bin-的Debug文件夹

using System;
using System.Diagnostics; namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "ConsoleApp1.exe";
p.StartInfo.Arguments = "a b c";//用空格来分隔参数,传给cmd.exe 时相当于传了个包含 a,b,c的字符串数组
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
//p.StartInfo.CreateNoWindow = true;//不显示程序窗口?????
p.Start();
string output = p.StandardOutput.ReadToEnd();//获取cmd窗口的输出信息
Console.WriteLine(output);
Console.ReadLine();
}
}
}

修改一下ConsoleApp2

        static void Main(string[] args)
{ while ( == )
{
Console.Write("输入指令:");
string str = Console.ReadLine();
Process p = new Process();
p.StartInfo.FileName = "ConsoleApp1.exe";
p.StartInfo.Arguments = str;//用空格来分隔参数,传给cmd.exe 时相当于传了个包含 a,b,c的字符串数组
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
//p.StartInfo.CreateNoWindow = true;//不显示程序窗口?????
p.Start();
string output = p.StandardOutput.ReadToEnd();//获取cmd窗口的输出信息
p.WaitForExit();//等待程序执行完退出进程
p.Close();
Console.WriteLine("返回信息:" + output);
Console.WriteLine("");
}
}

就可以和调用的控制台交互了

但是有个问题,如果输入的字符串带空格,就会截取到空格前的字符输出

多加上双引号就可以表示成一个字符串

二、调用cmd

官方

        static void Main(string[] args)
{ //Console.WriteLine("Ready to sort one or more text lines..."); using (Process myProcess = new Process())
{
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true; myProcess.Start(); StreamWriter myStreamWriter = myProcess.StandardInput; String inputText;
do
{
//Console.WriteLine("Enter a line of text (or press the Enter key to stop):"); inputText = Console.ReadLine();
if (inputText.Length > )
{
myStreamWriter.WriteLine(inputText);
}
} while (inputText.Length > ); myStreamWriter.Close(); myProcess.WaitForExit();
}
Console.ReadLine();
}

最新文章

  1. ASP.NET MVC3中Controller与View之间的数据传递总结
  2. 理解HTTP协议
  3. GLine游戏(Win32GUI实现,CodeBlocks+GCC编译)
  4. [HDU5904]LCIS(DP)
  5. vs2012中怎样设为起始页,怎样取消
  6. html的解析
  7. 个人简历制作(Dreamweaver)
  8. CreateProcess error=206, The filename or extension is too long"的一个解决方案
  9. web服务器、应用服务器、http服务器区别
  10. Div+Css的初步运用
  11. GridView分页的实现
  12. 前端性能优化:使用Data URI代替图片SRC
  13. HTML5之部分显示
  14. JS扩展方法
  15. Exec sql/c
  16. Dalvik虚拟机的优化机制
  17. 新秀学习SSH(十四)——Spring集装箱AOP其原理——动态代理
  18. Struts2学习笔记④
  19. (转)css内边距与外边距的区别,精辟啊
  20. AI - TensorFlow - 过拟合(Overfitting)

热门文章

  1. thead tfoot tbody标签的使用
  2. oracle 引用类型声明
  3. BZOJ4591 SHOI2015超能粒子炮·改(卢卡斯定理+数位dp)
  4. C语言的getopt
  5. maven项目在eclipse tomcat正常运行
  6. POJ 2456 Aggressive cows---二分搜索法
  7. jvm面试必会基本知识
  8. CVE-2016-6662 mysql RCE测试
  9. ubuntu安装mysqlclient
  10. python面向对象之继承与派生