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. 运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决

    运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决 在Idea中,直接运行ComprehensiveExample.scala,报需要指定master ...

  2. C#写文本文件,如何换行(添加换行符)

    把文本写到文件中,如果是几段文字拼合起来输出到文件中,通常每段非结尾文字后需要添加换行符,不然几段文字都变成一段. 在 C# 中,文本换行有两种方法,一种在需要换行的文本后面添加换行符 \r\n 即可 ...

  3. ADO.NET操作PostgreSQL:数据库操作类(已封装)

    1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...

  4. 获取微信签名,并保存在xml文件中

    using System; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using Sys ...

  5. 如何: 在 VS中的设计时刻主从表绑定控件到数据库

    这个示例展示了如何在 Visual Studio 2005 的设计时刻,把一个 data-aware 控件 (XtraGrid.XtraPivotGrid.XtraVerticalGrid 等) 绑定 ...

  6. 欢迎使用CSDN-markdown编辑器u

    这里写自定义目录标题 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一 ...

  7. 标准的sql执行顺序

    正常情况下是先join再进行where过滤

  8. 四,php异常处理

    1,异常处理 异常处理用于在指定的异常或错误发生时,改变脚本的正常执行流程. <?php try{ //错误或异常 }catch (Exception $ex){ //处理异常 //抛出异常 } ...

  9. 三:MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下 <?xml version="1. ...

  10. 【NOIP2016提高组】 Day2 T3 愤怒的小鸟

    题目传送门:https://www.luogu.org/problemnew/show/P2831 说个题外话:NOIP2014也有一道题叫做愤怒的小鸟. 这题自测时算错了eps,导致被卡了精度,从1 ...