using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net.NetworkInformation; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace testNetwork { public partial class Form1 : Form { public Form1() { InitializeComponent(); } System.IO.StreamWriter file; bool run = false; private void button1_Click(object sender, EventArgs e) { file =new System.IO.StreamWriter("log.txt"); run = true; backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while (run) { PingIt(); Thread.Sleep(10000); } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { label1.Text = message; } string message; public void PingIt() { Ping p = new Ping(); var ip = textBox1.Text; string time=DateTime.Now.ToString("h:mm:ss tt"); PingReply r = p.Send(ip); if (r.Status == IPStatus.Success) message = string.Format("{1} IP:{0} pint test ok!", ip, time); else message = string.Format("{1} IP:{0} pint test failed!", ip, time); backgroundWorker1.ReportProgress(0); try { file.WriteLine(message); } catch { } } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { run = false; file.Flush(); file.Close(); } } }
2015年8月26日 星期三
[C#] ping指定ip寫入log檔
最近做了一個小程式,每隔10秒用來ping某個指定IP確認網路穩定性,並將結果寫入txt做紀錄。
沒有留言:
張貼留言