C#获取命令行输出内容的方法
获取命令行输出内容的方式有传统和异步两种方式。
传统方式:
public static void RunExe(string exePath, string arguments, out string output, out string error)
{
using (Process process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = arguments;
// 必须禁用操作系统外壳程序
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true; process.Start(); output = process.StandardOutput.ReadToEnd();
error = process.StandardError.ReadToEnd(); process.WaitForExit();
process.Close();
}
}
class Program
{
static void Main(string[] args)
{
string output;
string error;
CMD.RunExe("ping", "www.baidu.com",out output,out error);
Console.WriteLine(output +"\n"+ error);
}
}
异步方式:
/// <summary>
/// 执行一条command命令
/// </summary>
/// <param name="command">需要执行的Command</param>
/// <param name="output">输出</param>
/// <param name="error">错误</param>
public static void ExecuteCommand(string command, out string output, out string error)
{
try
{
//创建一个进程
Process process = new Process();
process.StartInfo.FileName = command; // 必须禁用操作系统外壳程序
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true; //启动进程
process.Start(); //准备读出输出流和错误流
string outputData = string.Empty;
string errorData = string.Empty;
process.BeginOutputReadLine();
process.BeginErrorReadLine(); process.OutputDataReceived += (sender, e) =>
{
outputData += (e.Data + "\n");
}; process.ErrorDataReceived += (sender, e) =>
{
errorData += (e.Data + "\n");
}; //等待退出
process.WaitForExit(); //关闭进程
process.Close(); //返回流结果
output = outputData;
error = errorData;
}
catch (Exception)
{
output = null;
error = null;
}
}
C#获取命令行输出内容的方法的更多相关文章
- python获取命令行输出结果
#coding=utf-8 import os command = 'ping www.baidu.com ' #可以直接在命令行中执行的命令 r = os.popen(command) #执行该 ...
- python学习 —— 使用subprocess获取命令行输出结果
这里使用的版本:Python2 >= 2.7 对于获取命令行窗口中的输出python有一个很好用的模块:subprocess 两个简单例子: 1.获取ping命令的输出: from subpro ...
- C/C++ 程序中调用命令行命令并获取命令行输出结果
在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果.例如system(“ls”)只能得到0或非0,如果要获得ls的执行结 ...
- Delphi 获取命令行输出的函数
function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string; var SA: TSecurityAttribute ...
- python 获取命令行输出结果
status, output = commands.getstatusoutput("sudo rm -rf a.txt") if(not status): print(" ...
- python获取命令行参数的方法(汇总)
介绍python获取命令行参数的方法:getopt模和argparse模块. python版本:2.7 一.getopt模块 主要用到了模块中的函数: options, args = getopt.g ...
- 【Python学习 】Python获取命令行参数的方法
背景 最近编写一个python程序的时候,需要去获取python命令行的参数,因此这里记录下如何获取命令行参数的方法. 一.sys 模块 在 Python 中,sys 模块是一个非常常用且十分重要的模 ...
- MFC中获取命令行参数的几种方法
在MFC程序中,可以用以下几种方法来获取命令行参数. 为方便说明,我们假设执行了命令:C:\test\app.exe -1 -2 方法一 ::GetCommandLine(); 将获取到 " ...
- WordPress 无法使用the_content()方法输出内容
在使用WordPress里在一个页面里我使用the_content()方法来输出当前页面的内容,但却显示为空,而标题,url等都没有问题 在网络上好像遇到这种情况的人很少只找到了一个说是可能是func ...
随机推荐
- 一个五年 Android 开发者百度、阿里、聚美、映客的面试心经
花絮 也许会有人感叹某些人的运气比较好,但是他们不曾知道对方吃过多少苦,受过多少委屈.某些时候就是需要我们用心去发现突破点,然后顺势而上,抓住机遇,那么你将会走向另外一条大道,成就另外一个全新的自我. ...
- 软件产品案例分析----K米app
第一部分 调研.评测 1.1评测 1.1.1 第一次上手体验 1.1.2 bug 1.1.2.1 bug定义 1.1.2.2 栗子 1.1.2.2.1 A级 1.1.2.2.2 B级 1.1.2.2. ...
- 扩展htmlhelper.DropDownListFor 支持list数据源和option增加属性
mvc自带的DropDownListFor数据源必须是IEnumerable<SelectListItem>.并且option不支持增加自定义属性.在使用bootstrap-select组 ...
- 关于php自带的访问服务器xml的方法的坑
就据我了解,php中有两种读取读取xml文件的方法,我就简单介绍一下, 一种是使用simplexml_load_file($src)读取xml文件.simplexml_load_file会把该函数参数 ...
- bzoj4462: [Jsoi2013]编程作业
KMP还是有点用处的嘛qwq 对于小写字母,修改其为前一个这个小写字母和它的距离 然后跑KMP就行了 跑得飞快 #include <iostream> #include <cstdi ...
- COGS729. [网络流24题] 圆桌聚餐
«问题描述:假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri(i=1,2,3...m), .会议餐厅共有n张餐桌,每张餐桌可容纳c i(i=1,2...n) 个代表就餐.为了 ...
- qthread 使用 signal 方法通信
因为之间尝试过的 signal 机制,都是在 emit singnal_my() 的地方,直接调用了 slot 函数:相当于,slot 只是一个回调函数. 所以,在这里有点困惑,如果是要顺序执行完 s ...
- Linux ext3 ext4 区别
Linux kernel 自 2.6.28 开始正式支持新的文件系统 Ext4. Ext4 是 Ext3 的改进版,修改了 Ext3 中部分重要的数据结构,而不仅仅像 Ext3 对 Ext2 那样,只 ...
- SDL第一个程序:加载一张图片
直接看代码吧 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...
- AE开发能否实现TOC Control里添加多个Data Frame
问题: 在ArcMap中,菜单Insert下Data Frame,可以在TOC中增加Data Frame,在MapControl或者PageLayoutControl下都可以正常显示多个Data Fr ...