demo1:
        /// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="append">是否是追加</param>
private void ShowOutput(string str,bool append) {
if (this.txtOutput.InvokeRequired)
{
this.txtOutput.Invoke(new MethodInvoker(() => {
if (append)
{
this.txtOutput.AppendText(str);
this.txtOutput.AppendText(System.Environment.NewLine);
}
else
{
this.txtOutput.Clear();
} }));
}
else
{
if (append)
{
this.txtOutput.AppendText(str);
this.txtOutput.AppendText(System.Environment.NewLine);
}
else
{
this.txtOutput.Clear();
}
}
} private void btnRun_Click(object sender, EventArgs e)
{ Thread thread = new Thread(() => { ShowOutput("",false);
//this.txtOutput.Clear();
ProcessStartInfo psi = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
psi.Arguments = this.txtMachine.Text;//设置命令参数
psi.CreateNoWindow = true;//不显示dos命令行窗口
psi.RedirectStandardOutput = true;//
psi.RedirectStandardInput = true;//
psi.UseShellExecute = false;//是否指定操作系统外壳进程启动程序 Process p = Process.Start(psi);
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
;
ShowOutput(line,true);
line = reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流 }); thread.IsBackground = true;
thread.Start(); } private void Form1_Load(object sender, EventArgs e)
{
this.txtMachine.Text = "127.0.0.1";
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

 

 

demo2:

        /// <summary>
/// appoint exe
/// </summary>
/// <param name="exeStr"></param>
/// <param name="fileStr"></param>
public void OpenFile(string exeStr,string fileStr) {
//ProcessStartInfo
ProcessStartInfo psi;
if (String.IsNullOrEmpty(exeStr))
{
psi = new ProcessStartInfo();
}
else
{
psi = new ProcessStartInfo(exeStr);//应用程序或文档名
} psi.Arguments = fileStr; Process process = new Process();
process.StartInfo = psi;
process.Start(); } public void OpenFile(string fileStr)
{
OpenFile(null, fileStr);
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

 

demo3:

        public static void PintTest()
{
//Allows an application to determine whether a remote computer is accessible over the network.
Ping pingSender = new Ping(); // Create a buffer of 32 bytes of data to be transmitted.
string data = "sendMsg";
byte[] buffer = Encoding.ASCII.GetBytes(data); // Wait 10 seconds for a reply.
int timeout = 10000; // Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions(64, true); // Send the request.
PingReply reply = pingSender.Send("www.baidu.com", timeout, buffer, options); if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address.ToString());
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
//A Byte array containing the data received in an ICMP echo reply message, or an empty array, if no reply was received.
Console.WriteLine("Received in an ICMP echo reply message: {0}", Encoding.Default.GetString(reply.Buffer));
}
else
{
Console.WriteLine(reply.Status);
}
}

c#调用本地命令并截取Output的更多相关文章

  1. Android 中调用本地命令

    Android 中调用本地命令 通常来说,在 Android 中调用本地的命令的话,一般有以下 3 种情况: 调用下也就得了,不管输出的信息,比如:echo Hello World.通常来说,这种命令 ...

  2. Java调用本地命令

    参考:http://blog.csdn.net/zhu_xun/article/details/19539513 http://www.cnblogs.com/kingcucumber/p/31801 ...

  3. java命令行调用本地文件协议hikvideoclient://

    最近在做一个视频项目,项目中需要通过调用海康本地协议打开视频播放器,起初尝试通过Process/ProcessBuilder无解,因为这个是调用本地应用程序的. 我要调用的是本地伪协议,最终通过一些研 ...

  4. [转载]java调用本地dos命令

    在社区看到java调用本地dos命令的代码,特贴出来 String command = "ipconfig"; Runtime run = Runtime.getRuntime() ...

  5. js网页中调用本地应用程序

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Con ...

  6. python 调用 shell 命令方法

    python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等   ...

  7. Android使用JNI(从java调用本地函数)

    当编写一个混合有本地C代码和Java的应用程序时,需要使用Java本地接口(JNI)作为连接桥梁.JNI作为一个软件层和API,允许使用本地代码调用Java对象的方法,同时也允许在Java方法中调用本 ...

  8. Delphi 调用netsh命令修改IP地址

    Delphi 调用netsh命令修改IP地址 先介绍一下Netsh命令的使用方法: 在这里跟大家介绍几个简单的指令 1.Show IP 1.1Cmd Mode 直接在cmd下面输入 netsh int ...

  9. 分享:Svg文件转换为图片(调用 Inkscape 命令行)

    其实只是做了简单封装,可以方便进行批量转换. 获取Svg对象坐标的代码请看:根据svg节点对象类型和路径值转换坐标值, DrawingColor方法是进行颜色填充的. /// <summary& ...

随机推荐

  1. BitAdminCore框架更新日志20180518

    20180518更新内容 1.重构调整QQ登录代码,使用JObject,减少代码,增加access_token自动续期(未测试). 2.重构调整微信登录代码,使用JObject,减少代码,增加acce ...

  2. T-SQL判断是否存在表、临时表

    利用SQL SERVER的系统函数 object_id() 可以判断是否存在表.临时表, object_id() 的作用是返回架构范围内对象的数据库对象标识.(即返回系统视图  sys.objects ...

  3. log Log4NET配置

    Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过 日志查看系统的运行过程,从而发现系统的问题.日志的作用:将 ...

  4. 792. Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  5. AVL树的实现——c++

    一.概念 AVL树是根据它的发明者G.M. Adelson-Velsky和E.M. Landis命名的.它是最先发明的自平衡二叉查找树,也被称为高度平衡树.相比于"二叉查找树",它 ...

  6. zoj3497 Mistwald(矩阵快速幂)

    题意:给定一个有向图(最多25个节点,每个节点的出度最多为4),给定起点和终点,然后从起点开始走,走到终点就停止,否则一直往下走,问能不能P步到达终点.也就是说从起点出发,走一条长度为P的路径,路径中 ...

  7. express form/ajax 后端获取前端数据

    -------------------2017/12/02补充:缺了一个重要条件... var bodyParser = require('body-parser');var app = expres ...

  8. SpringBoot入门之分散配置

    springboot默认支持两种格式的配置文件:.properties和.yml.其中.properties是属性文件,也是最常用的一种:.yml是yaml格式的文件,yaml是一种简洁的标记语言.例 ...

  9. JavaScript函数学习总结(一)---函数定义

    博客原文地址:Claiyre的个人博客 如需转载,请在文章开头注明原文地址 在许多传统的OO语言中,对象可以包含数据,还可拥有方法,也就是属于该对象的函数.但在JavaScript中,函数也被认为是一 ...

  10. 当需要给<span>标签设宽度

    span{ display: inline-block; width: 100px; } 当需要实现横排单行文字,用到span并且想设宽度的时候, 用以上css来实现.