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& ...
随机推荐
- PostgreSQL 用户和权限管理
PostgreSQL 用户和权限管理 创建 CREATE ROLE rolename;CREATE USER username;CREATE USER和CREATE ROLE的区别在于,CREATE ...
- Transaction And Lock--唯一索引下INSERT导致的死锁
背景: 曾经的一位同事问我:"数据库只有并发INSERT 操作,会造成死锁么?",我没有太多思考地回答"不会",但真的不会吗? 测试: --========== ...
- json--pyton中obj与json的互转,js中obj与json的互转
json 解释:json是一种跨平台的通用的数据格式 python中对象(obj)与json之间的相互转换 1.对象(obj)转json格式的字符串 json.dumps(res) res = () ...
- C#克隆
克隆方法是原型设计模式中必须使用的方式,它将返回一个与当前对象数据一致的对象.正如其名,犹如一个模子雕刻而出.克隆类型分为两种:浅克隆.深克隆. 1.浅克隆 浅克隆方式是最简单.最直接的方式.只需要类 ...
- 为什么子元素设置margin-top会作用在父元素上?
原因在于:CSS 外边距合并 复现: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- falcon nodata 小坑一枚
按照官方文档配置完一切正常,唯独 nodata, 明明有正常的数据,但是为什么 nodata 会认为是没收到呢 困扰许久,直到看了数据库中的数据才恍然大悟 falcon_portal库中的 hosts ...
- [JavaScript] 将字符串数组转化为整型数组
var dataStr="1,2,3,4,5";//原始字符串 var dataStrArr=dataStr.split(",");//分割成字符串数组 var ...
- Java零基础教程(一)环境搭建
本文将带领您一步一步地搭建Java开发环境 一.认识什么是Java Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Window ...
- Swift里字符串(六)Shared strings
Shared strings do not have tail-allocated storage, but can provide access upon query to contiguous U ...
- ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Struts2未配置Log4j2.xml报错 Log4j2.xml中的配置 log4j的jar包:log4j-core-2.7.jar log4j2只支持xml和json两种格式的配置,所以配置log ...