定期的にtracertを実行したかったので。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
using System; using System.Drawing; using System.IO; using System.Text.RegularExpressions; using System.Diagnostics; using System.Windows.Forms; namespace Tracert { public partial class Form1 : Form { public Form1() { InitializeComponent(); WindowState = FormWindowState.Minimized; ShowInTaskbar = false; Bitmap bitmap = new Bitmap(32, 32); Graphics graphics = Graphics.FromImage(bitmap); graphics.FillRectangle(Brushes.Green, graphics.VisibleClipBounds); NotifyIcon notifyIcon = new NotifyIcon(); notifyIcon.Icon = Icon.FromHandle(bitmap.GetHicon()); notifyIcon.Visible = true; notifyIcon.Click += new EventHandler((object sender, EventArgs e) => { MessageBox.Show("終了します。"); Application.Exit(); }); Timer timer = new Timer(); timer.Interval = 3600000; timer.Tick += new EventHandler((object sender, EventArgs e)=> { Execute(); }); timer.Start(); } private void Execute() { using (Process process = new Process()) { process.StartInfo.RedirectStandardOutput = true; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "tracert"; process.StartInfo.Arguments = "192.168.5.1"; process.Start(); string result = ""; using (StringReader sr = new StringReader(process.StandardOutput.ReadToEnd())) { Regex regex = new Regex(@"ms"); while (sr.Peek() > -1) { string ln = sr.ReadLine(); if (regex.IsMatch(ln)) result += ln; } } process.WaitForExit(); result = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + result; using (StreamWriter sw = new StreamWriter(Application.StartupPath + @"\dat.txt", true, System.Text.Encoding.UTF8)) { sw.Write(result + "\r\n"); } } } } } |