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. SQL分组合并

    STUFF ( character_expression , start , length ,character_expression ) select TcodMedInst_GUID,stuff( ...

  2. json--pyton中obj与json的互转,js中obj与json的互转

    json 解释:json是一种跨平台的通用的数据格式 python中对象(obj)与json之间的相互转换 1.对象(obj)转json格式的字符串 json.dumps(res) res = () ...

  3. 【cocos2d-x 手游研发----研发思路及感想】

          我半年前进入了目前的这家做教育行业的公司(在此之前一直从事原生态开发手游的迷茫之路),学习是一件很快乐的事情,来到这家公司我有了很多时间去学习,不管是公司业务,还是其他技术相关的.于是开始 ...

  4. Day 61 Django第二天 (orm数据库操作)

    一.get请求和post请求 GET请求: 1. 浏览器请求一个页面 2. 搜索引擎检索关键字的时候 POST请求: 1. 浏览器向服务端提交数据,比如登录/注册等 二 . Django中的APP: ...

  5. 因子和(luoguP1593)(等比数列求和+逆元)

    输入两个正整数\(a\)和\(b\),求\(a\cdot b\)的因子和.结果太大,只要输出它对9901的余数. Input 仅一行,为两个正整数\(a\)和\(b\)(\(0≤a,b≤5000000 ...

  6. ssh 登陆 端口转发

    man ssh ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] ...

  7. 完整的REM布局的工作流程与规范

    rem从去年的手淘双11开始火起来之后一直就想去使用,但是苦于学习途径有限,工作任务也比较繁忙导致一度延后. 那么现在对相关知识的学习与初步的项目实践之后,在这里记录一下使用rem解决各屏幕适配问题. ...

  8. 《Vue 编程房内考》

    古人有云:码农爱coding,则为之计深远. 众人问:何为之? 古人曰:底层.算法和架构. 众木然. 古人又曰:多看源码. 以下内容是我在学习 Vue-2.5.2 源码时的一个总结. 第一章 活捉Vu ...

  9. 00-python概述。

    人生苦短,我用Python. -发展历史: - 1989年,由Guido van Rossum开始开发, - 1991年,发布第一个公开发行版,第一个Python编译器(同时也是解释器)诞生. - 2 ...

  10. Alamofire源码导读五:错误表示

    AFError is the error type returned by Alamofire. It encompasses a few different types of errors, eac ...