C# 用程序读写另一个控制台程序
一、
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# 用程序读写另一个控制台程序的更多相关文章
- VC2012+QT新建一个控制台程序
1.新建一个项目,选择控制台程序 2.下一步.project setting 可以包含模块,可以再这选择也可以之后选择 3.配置工程属性 1)需要源码的话添加VC++目录里的源目录 2)包含头文件 ...
- android intent隐式调用之一个应用程序启动另一个应用程序
理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似:另一种是隐式的Intent,即Inten ...
- Android学习笔记_70_一个应用程序启动另一个应用程序的Activity
第一种(我自己写的) :之前在网上看来一些,很多不是我要的可以启动另外一个应用程序的主Activity. //这些代码是启动另外的一个应用程序的主Activity,当然也可以启动任意一个Activit ...
- iOS 在一个应用程序中调另一个应用程序
在A应用程序中调用B应用程序 1. 首先在B应用程序中生成URL 1)点击targets文件 2)点击Info 3)生成URL ①在Info.plist文件中点击+(新添加一项) ②在Info.pli ...
- C#零基础入门-3-第一个控制台程序
打开VS2017 文件 新建 项目 模板选择Visual C# Windows 控制台应用程序 快速写入Console.WriteLine 输入cw,然后快速按tab键两次即可.
- CreateProcess执行一个控制台程序,隐藏窗口
STARTUPINFO StartupInfo;//创建进程所需的信息结构变量 PROCESS_INFORMATION ProcessInfo; GetStartupInfo(&Sta ...
- [idea]创建一个控制台程序
新建项目时,选择JBoss即可.
- 微信小程序开发2-第一个小程序开发准备
1.首先在官网上注册一个账号( https://mp.weixin.qq.com/ )申请一个AppID(类似于人的身份证,小程序也需要身份证) 注册过程不多说 2.安装开发工具( https://m ...
- qt console 控制台程序 与win console控制台程序是不同的
#include <QtCore/QCoreApplication> int main(int argc, char *argv[]){ QCoreApplication a(argc, ...
随机推荐
- JS判断页面是否加载完成
用 document.readyState == "complete" 判断页面是否加载完成 传回XML 文件资料的目前状况. 基本语法intState = xmlDocument ...
- Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem
D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...
- eclipse web(Spring+SpringMVC+Hibernate)项目迁移至intellij idea
1.导入Eclipseweb项目 跟着导航一直下一步 出现警告不要担心,先点击确认,到后面再进行设置jdk 成功导入项目后如下图 2.对导入的项目进行配置按Ctrl+shift+alt+s(或下图中的 ...
- The base command for the Docker CLI.
Description The base command for the Docker CLI. Child commands Command Description docker attach At ...
- Installing Jenkins to Centos Docker
1.Install Docker CE to Centos7 [root@zoo1 ~]# yum install -y yum-utils device-mapper-persistent-data ...
- 解决在极光推送的时候会出现一个 JPush提示:缺少统计代码
<span style="font-size:14px;"> @Override protected void onResume(){ super.onResume() ...
- hive向表格中插入数据并分析语句
1,---导入mds_imei_month_info ; //最大的动态分区表 set hive.support.concurrency=false; //是否支持并发 ; //each mapper ...
- 【BZOJ2338】【HNOI2011】数矩形 [计算几何]
数矩形 Time Limit: 20 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 最近某歌手在研究自己的全国巡回演出, ...
- 【BZOJ】1782: [Usaco2010 Feb]slowdown 慢慢游
[算法]DFS序+树状数组 [题解]题意相当于统计前i-1个点在第i个点的祖先的个数,显然可以用dfs维护,用树状数组差分维护前缀和. 出栈不新加节点就要注意左闭右开,即in[a[i]]处+1,ou[ ...
- 最短路算法详解(Dijkstra,Floyd)
最短路径 在一个无权的图中,若从一个顶点到另一个顶点存在着一条路径,则称该路径长度为该路径上所经过的边的数目,它等于该路径上的顶点数减1.由于从一个顶点到另一个顶点可能存在着多条路径,每条路径上所经过 ...