一、

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. Concurrent.util中的一些类

    package com.bjsxt.height.concurrent019; import java.io.IOException; import java.util.Random; import ...

  2. JSP中的Cookie

    如何创建Cookie 先引包: import="javax.servlet.http.Cookie" JSP是使用如下的语法格式来创建cookie的: Cookie cookie_ ...

  3. BZOJ 2820: YY的GCD | 数论

    题目: 题解: http://hzwer.com/6142.html #include<cstdio> #include<algorithm> #define N 100000 ...

  4. timeSetEvent()函数

    原文链接地址:http://www.cnblogs.com/kangwang1988/archive/2010/09/16/1827872.html 微软公司在其多媒体Windows中提供了精确定时器 ...

  5. React生命周期总结

    React的生命周期总共8个钩子,三个will,两个Did,一个RecciveProps,一个ShouldUpdate,一个render.分为三个阶段,分别是 装载 Mounting更新 Updati ...

  6. 如何用setInterval调用类的方法

    setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭.由 se ...

  7. transition(动画属性)

    CSS 过渡(transition)是通过定义元素从起点的状态和结束点的状态,在一定的时间区间内实现元素平滑地过渡或变化的一种补间动画机制.你可以让属性的改变过程持续一段时间,而不是立即生效. 通过t ...

  8. Android推送使用--文章集锦

    Android之基于百度云推送IM Android实现推送方式解决方案 Android消息推送(一)--AndroidPn(XMPP协议)Demo版到正式上线 采用XMPP协议实现Android推送 ...

  9. LOJ tangjz的背包

    题目大意 有 \(n\) 个物品, 第 \(i\) 个物品的体积为 \(i\) 令 \(f(x)\) 为 选择 \(m\) 个物品, 体积和为 \(x\) 的方案数 令 \(V = \sum_{i=1 ...

  10. The 2017 ACM-ICPC Asia East Continent League Final记录

    首先感谢tyz学弟的麻麻-给我们弄到了名额- 然后就开始了ACM ECLFinal的玩耍,A*仙人掌可是立了flag要好好打的- 试机赛好像就全是GCJ kickstart的原题,然后AK了但是由于一 ...