c#调用本地命令并截取Output
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的更多相关文章
- Android 中调用本地命令
Android 中调用本地命令 通常来说,在 Android 中调用本地的命令的话,一般有以下 3 种情况: 调用下也就得了,不管输出的信息,比如:echo Hello World.通常来说,这种命令 ...
- Java调用本地命令
参考:http://blog.csdn.net/zhu_xun/article/details/19539513 http://www.cnblogs.com/kingcucumber/p/31801 ...
- java命令行调用本地文件协议hikvideoclient://
最近在做一个视频项目,项目中需要通过调用海康本地协议打开视频播放器,起初尝试通过Process/ProcessBuilder无解,因为这个是调用本地应用程序的. 我要调用的是本地伪协议,最终通过一些研 ...
- [转载]java调用本地dos命令
在社区看到java调用本地dos命令的代码,特贴出来 String command = "ipconfig"; Runtime run = Runtime.getRuntime() ...
- js网页中调用本地应用程序
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Con ...
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- Android使用JNI(从java调用本地函数)
当编写一个混合有本地C代码和Java的应用程序时,需要使用Java本地接口(JNI)作为连接桥梁.JNI作为一个软件层和API,允许使用本地代码调用Java对象的方法,同时也允许在Java方法中调用本 ...
- Delphi 调用netsh命令修改IP地址
Delphi 调用netsh命令修改IP地址 先介绍一下Netsh命令的使用方法: 在这里跟大家介绍几个简单的指令 1.Show IP 1.1Cmd Mode 直接在cmd下面输入 netsh int ...
- 分享:Svg文件转换为图片(调用 Inkscape 命令行)
其实只是做了简单封装,可以方便进行批量转换. 获取Svg对象坐标的代码请看:根据svg节点对象类型和路径值转换坐标值, DrawingColor方法是进行颜色填充的. /// <summary& ...
随机推荐
- MYC编译器源码之词法分析
前文 .NET框架源码解读之MYC编译器 和 MYC编译器源码分析之程序入口 分别讲解了 SSCLI 里示例编译器的架构和程序入口,本文接着分析它的词法分析部分的代码. 词法解析的工作都由Tok类处 ...
- C#基础复习(1) 之 Struct与Class的区别
参考资料 [1] 毛星云[<Effective C#>提炼总结] https://zhuanlan.zhihu.com/p/24553860 [2] <C# 捷径教程> [3] ...
- Django分页设置
1. """ 分页组件使用示例: obj = Pagination(request.GET.get('page',1),len(USER_LIST),request.pa ...
- Day 62 Django第三天
2.GET请求能够被cache,GET请求能够被保存在浏览器的浏览历史里面(密码等重要数据GET提交,别人查看历史记录,就可以直接看到这些私密数据)POST不进行缓存. 3.GET参数是带在URL后面 ...
- Day 24 继承
# 一.什么是继承 # 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可成为基类或超类,新建类称为派生类或子类.# python中类的继承分为:单继承和多继承cla ...
- Cordova - 解决升级NDK之后无法编译!
先前Cordova工作正常,今天升级了一下Android Studio,随后发现NDK也有升级包了,于是顺利升级了NDK!升级完毕发现,无法正常编译了,错误提示如下: No toolchains fo ...
- Android NDK开发Crash错误定位
在Android开发中,程序Crash分三种情况:未捕获的异常.ANR(Application Not Responding)和闪退(NDK引发错误).其中未捕获的异常根据logcat打印的堆栈信息很 ...
- linux对文件赋权限的命令chmod的详细说明
指令名称 : chmod使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案调 ...
- node 利用http和cheerio编写简易爬虫
首先cnpm init创建一个package.json 引入cheerio模块 cnpm install --save cheerio 然后开始编写代码 let cheerio = require(' ...
- js集合set类的实现
js集合set类的实现 /*js集合set类的实现*/ function Set() { this.dataStore = []; this.add = add;//新增元素 this.remove ...