一、

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();
}

C# 用程序读写另一个控制台程序的更多相关文章

  1. VC2012+QT新建一个控制台程序

    1.新建一个项目,选择控制台程序 2.下一步.project setting 可以包含模块,可以再这选择也可以之后选择 3.配置工程属性 1)需要源码的话添加VC++目录里的源目录 2)包含头文件   ...

  2. android intent隐式调用之一个应用程序启动另一个应用程序

    理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似:另一种是隐式的Intent,即Inten ...

  3. Android学习笔记_70_一个应用程序启动另一个应用程序的Activity

    第一种(我自己写的) :之前在网上看来一些,很多不是我要的可以启动另外一个应用程序的主Activity. //这些代码是启动另外的一个应用程序的主Activity,当然也可以启动任意一个Activit ...

  4. iOS 在一个应用程序中调另一个应用程序

    在A应用程序中调用B应用程序 1. 首先在B应用程序中生成URL 1)点击targets文件 2)点击Info 3)生成URL ①在Info.plist文件中点击+(新添加一项) ②在Info.pli ...

  5. C#零基础入门-3-第一个控制台程序

    打开VS2017 文件 新建 项目 模板选择Visual C# Windows 控制台应用程序 快速写入Console.WriteLine 输入cw,然后快速按tab键两次即可.

  6. CreateProcess执行一个控制台程序,隐藏窗口

    STARTUPINFO   StartupInfo;//创建进程所需的信息结构变量 PROCESS_INFORMATION   ProcessInfo; GetStartupInfo(&Sta ...

  7. [idea]创建一个控制台程序

    新建项目时,选择JBoss即可.

  8. 微信小程序开发2-第一个小程序开发准备

    1.首先在官网上注册一个账号( https://mp.weixin.qq.com/ )申请一个AppID(类似于人的身份证,小程序也需要身份证) 注册过程不多说 2.安装开发工具( https://m ...

  9. qt console 控制台程序 与win console控制台程序是不同的

    #include <QtCore/QCoreApplication> int main(int argc, char *argv[]){ QCoreApplication a(argc, ...

随机推荐

  1. 【bzoj1465/bzoj1045】糖果传递 数论

    题目描述 老师准备了一堆糖果, 恰好n个小朋友可以分到数目一样多的糖果. 老师要n个小朋友去拿糖果, 然后围着圆桌坐好, 第1个小朋友的左边是第n个小朋友, 其他第i个小朋友左边是第i-1个小朋友. ...

  2. 【算法】01分数规划 --- HNOI2009最小圈 & APIO2017商旅 & SDOI2017新生舞会

    01分数规划:通常的问法是:在一张有 \(n\) 个点,\(m\) 条边的有向图中,每一条边均有其价值 \(v\) 与其代价 \(w\):求在图中的一个环使得这个环上所有的路径的权值和与代价和的比率最 ...

  3. 洛谷P4593 [TJOI2018]教科书般的亵渎 【数学】

    题目链接 洛谷P4593 题解 orz dalao upd:经典的自然数幂和,伯努利数裸题 由题我们只需模拟出代价,只需使用\(S(n,k) = \sum\limits_{i = 1}^{n} i^{ ...

  4. Conjugate 解题报告

    Conjugate 问题描述 在不存在的 \(\text{noip day3}\) 中,小 \(\text{w}\) 见到了一堆堆的谜题. 比如这题为什么会叫共轭? 他并不知道答案. 有 \(n\) ...

  5. angular-translate加载.json文件进行翻译

    这是这个demo的目录结构,总共有两个文件:locale-chinese.json和translation11.html locale-chinese.json文件的内容是: { "beau ...

  6. .NET之特性和属性(转)

    1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定 ...

  7. DotNETCore 学习笔记 日志

    Logging --------------------------------------------------------------------------------------- Impl ...

  8. mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1

    mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1 使用 sho ...

  9. HDU1284 钱币兑换问题

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. OpenWRT介绍

    1. 介绍 OpenWRT是一款第三方路由器固件, 其特别在于开放性, 如它的文件系统可写, 用户可在路由器上安装需要的第三方软件.通过刷入OpenWRT, 我们可以完成如下事情 - DLNA共享 - ...