C#中运行命令行截取输出流的例子
{
partial class CMDForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtCommand = new System.Windows.Forms.TextBox();
this.btnExecute = new System.Windows.Forms.Button();
this.tbResult = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 11);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 0;
this.label1.Text = "ping";
//
// txtCommand
//
this.txtCommand.Location = new System.Drawing.Point(41, 8);
this.txtCommand.Name = "txtCommand";
this.txtCommand.Size = new System.Drawing.Size(269, 21);
this.txtCommand.TabIndex = 1;
//
// btnExecute
//
this.btnExecute.Location = new System.Drawing.Point(316, 6);
this.btnExecute.Name = "btnExecute";
this.btnExecute.Size = new System.Drawing.Size(75, 23);
this.btnExecute.TabIndex = 2;
this.btnExecute.Text = "执行";
this.btnExecute.UseVisualStyleBackColor = true;
this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);
//
// tbResult
//
this.tbResult.Location = new System.Drawing.Point(8, 47);
this.tbResult.Multiline = true;
this.tbResult.Name = "tbResult";
this.tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.tbResult.Size = new System.Drawing.Size(383, 292);
this.tbResult.TabIndex = 3;
//
// CMDForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 364);
this.Controls.Add(this.tbResult);
this.Controls.Add(this.btnExecute);
this.Controls.Add(this.txtCommand);
this.Controls.Add(this.label1);
this.Name = "CMDForm";
this.Text = "运行Command的例子";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtCommand;
private System.Windows.Forms.Button btnExecute;
private System.Windows.Forms.TextBox tbResult;
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace RunCMD
{
/**
* 作者:周公
* blog:http://blog.csdn.net/zhoufoxcn
* 日期:2007-07-07
*
* */
public partial class CMDForm : Form
{
public CMDForm()
{
InitializeComponent();
}
private void btnExecute_Click(object sender, EventArgs e)
{
tbResult.Text = "";
ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments = txtCommand.Text;//设置命令参数
start.CreateNoWindow = true;//不显示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
Process p=Process.Start(start);
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
tbResult.AppendText(line+" ");
line = reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流
}
}
}
C#中运行命令行截取输出流的例子相关推荐
- 使用 CliWrap 让C#中的命令行交互举重若轻
在代码中进行命令行交互是一个很常见的场景, 特别是在一些CI CD 自动化流程中, 在这之前我们会使用 System.Diagnostics.Process API, 现在有一个更灵活的工具 CliW ...
- mysql隐藏密码_MySQL在Linux系统中隐藏命令行中的密码的方法
在命令行中输入命令并不是一个好主意,会造成安全问题.但是如果你决定去写一个应用,而这个应用需要在命令行中使用密码或者其他敏感信息.那么,你能通过以下方法禁止系统的其他用户轻易的看到这些敏感数据 呢?, ...
- 如何在Java中解析命令行参数?
在Java中解析命令行参数的好方法是什么? #1楼 我不建议使用Apache Common CLI库,因为它是非线程安全的. 它使用带有静态变量和方法的有状态类来进行内部工作(例如OptionBuil ...
- linux安装virtualbox命令,在Linux中从命令行查找Virtualbox Version的方法
在我的Ubuntu无头服务器上使用Virtualbox时,我需要找到Virtualbox的版本,如果是GUI,我可以通过导航到Virtualbox->About->Help轻松找到它,但我 ...
- Windows中的命令行提示符里的Start命令执行路径包含空格时的问题
转自:http://www.x2009.net/articles/windows-command-line-prompt-start-path-space.html 当使用Windows 中的命令行提 ...
- JavaJDK中的命令行工具
JavaJDK中的命令行工具 本文内容主要学习自<深入理解Java虚拟机>,超赞的一本书强烈推荐! 一,常用JDK监控和故障处理工具 命令名称 全称 用途 jstat JVM Statis ...
- php 开启命令模式,如何启用PhpStorm中的命令行工具
本篇文章主要给大家介绍如何使用phpstorm中的命令行工具. PhpStorm下载地址: PhpStorm使用命令行工具,我们可以直接从IDE调用命令!在我们使用任何命令行工具之前,我们必须在设置中 ...
- linux命令 socket,如何从linux中的命令行向socket.io websocket发送消息?
是否可以使用linux中的命令行向我的localhost服务器(节点)发送socket.io消息?我不确定这是否可行--从稀缺的谷歌搜索结果来看,我猜这不可能或不复杂-- 我的socket.io代码如 ...
- mysql5.7.11 linux_CentOS 7 中以命令行方式安装 MySQL 5.7.11 for Linux Generic 二进制版本教程详解...
MySQL 目前的最新版本是 5.7.11,在 Linux 下提供特定发行版安装包(如 .rpm)以及二进制通用版安装包(.tar.gz).一般情况下,很多项目都倾向于采用二进制通用安装包形式来进行安 ...
最新文章
- 基于开源CA系统ejbca community 6.3.1.1构建私有CA管理数字证书
- MySQL的4中隔离级别
- CVPR2017: Learning Deep Context-aware Features over Body and Latent Parts for
- 分析单点登录cas的解决方式
- [转]Velocity脚本摘要
- python 代理类型说明
- SpringBoot项目实现配置实时刷新功能
- Linux 网络抓包
- 8086cpu学习笔记(1):系统结构
- c语言单循环赛制,循环赛日程安排问题(分治法)
- python设置代理_python使用代理proxy
- 忍无可忍,决定宁可错杀千万,也不漏掉一个,弹已出膛,剑已出鞘!
- 机器学习实战(三):Classification
- python for循环流程图_python流程图
- 火遍全网的chatGPT(文末有彩蛋)
- 奔驰激活carplay手机互联系统编程改装 成都蔚一名车汇
- android 新闻频道,GitHub - xiyy/TopNews: 一款Android新闻客户端,并提供电视台直播功能...
- vue 微信授权解决方案
- 选择恐惧症的福音之轮盘小程序展示
- C++ 单向链表 —— 初始化、插入、返回第一个节点、删除、查找、长度、打印、反转(逆序)