C#怎么執(zhí)行ping命令

本篇內(nèi)容主要講解“C#怎么執(zhí)行ping命令”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“C#怎么執(zhí)行ping命令”吧!

專業(yè)領(lǐng)域包括網(wǎng)站設(shè)計、成都網(wǎng)站制作、成都商城網(wǎng)站開發(fā)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)建站的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。

C#執(zhí)行ping命令

首先,我們用使用Process類,來創(chuàng)建獨立的進程,導(dǎo)入System.Diagnostics,using System.Diagnostics;

實例一個Process類,啟動一個獨立進程Process p = new Process();

Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法。

下面我們用到了他的幾個屬性:
◆設(shè)定程序名p.StartInfo.FileName = "cmd.exe";
◆關(guān)閉Shell的使用p.StartInfo.UseShellExecute = false;
◆重定向標(biāo)準(zhǔn)輸入p.StartInfo.RedirectStandardInput = true;
◆重定向標(biāo)準(zhǔn)輸出p.StartInfo.RedirectStandardOutput = true;
◆重定向錯誤輸出p.StartInfo.RedirectStandardError = true;
◆設(shè)置不顯示窗口p.StartInfo.CreateNoWindow = true;

上面幾個屬性的設(shè)置是比較關(guān)鍵的一步。

既然都設(shè)置好了那就啟動進程吧,p.Start();

C#執(zhí)行ping命令,輸入要執(zhí)行ping命令,這里就是ping了,
p.StandardInput.WriteLine("ping -n 1 www.iwebtrados.com.cn");
p.StandardInput.WriteLine("exit");

從輸出流獲取命令執(zhí)行結(jié)果,string strRst = p.StandardOutput.ReadToEnd();

利用C#執(zhí)行ping命令

這里我寫的是一個窗體程序。首先添加textbox,listbox,button控件,其中textbox錄入域名或IP,listbox顯示結(jié)果.

在button1_click事件鍵入

privatevoidbutton1_Click(objectsender,EventArgse)  {  Pingp1=newPing();//只是演示,沒有做錯誤處理  PingReplyreply=p1.Send(this.textBox1.Text);//阻塞方式  displayReply(reply);//顯示結(jié)果  }  privatevoiddisplayReply(PingReplyreply)//顯示結(jié)果  {  StringBuildersbuilder;  if(reply.Status==IPStatus.Success)  {  sbuilder=newStringBuilder();  sbuilder.Append(string.Format("Address:{0}",reply.Address.ToString()));  sbuilder.Append(string.Format("RoundTriptime:{0}",reply.RoundtripTime));  sbuilder.Append(string.Format("Timetolive:{0}",reply.Options.Ttl));  sbuilder.Append(string.Format("Don'tfragment:{0}",reply.Options.DontFragment));  sbuilder.Append(string.Format("Buffersize:{0}",reply.Buffer.Length));  listBox1.Items.Add(sbuilder.ToString());  }  }

也可以做異步的處理,修改button1_click,并添加PingCompletedCallBack方法

privatevoidbutton1_Click(objectsender,EventArgse)  {  Pingp1=newPing();  p1.PingCompleted+=newPingCompletedEventHandler(this.PingCompletedCallBack);  //設(shè)置PingCompleted事件處理程序  p1.SendAsync(this.textBox1.Text,null);  }  privatevoidPingCompletedCallBack(objectsender,PingCompletedEventArgse)  {  if(e.Cancelled)  {  listBox1.Items.Add("PingCanncel");  return;  }  if(e.Error!=null)  {  listBox1.Items.Add(e.Error.Message);  return;  }  StringBuildersbuilder;  PingReplyreply=e.Reply;  if(reply.Status==IPStatus.Success)  {  sbuilder=newStringBuilder();  sbuilder.Append(string.Format("Address:{0}",reply.Address.ToString()));  sbuilder.Append(string.Format("RoundTriptime:{0}",reply.RoundtripTime));  sbuilder.Append(string.Format("Timetolive:{0}",reply.Options.Ttl));  sbuilder.Append(string.Format("Don'tfragment:{0}",reply.Options.DontFragment));  sbuilder.Append(string.Format("Buffersize:{0}",reply.Buffer.Length));  listBox1.Items.Add(sbuilder.ToString());  }  }

到此,相信大家對“C#怎么執(zhí)行ping命令”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章標(biāo)題:C#怎么執(zhí)行ping命令
URL分享:http://muchs.cn/article38/ghepsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、小程序開發(fā)用戶體驗、網(wǎng)站策劃、微信小程序、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司