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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Text.RegularExpressions; namespace log_chk { public partial class Form1 : Form { NotifyIcon n; public Form1() { InitializeComponent(); WindowState = FormWindowState.Minimized; ShowInTaskbar = false; string path_icon = @"C:\Users\s.okamura\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\KJIGEKB9\favicon[3].ico"; path_icon = @"C:\Windows\System32\PerfCenterCpl.ico"; n = new NotifyIcon(this.components); n.Icon = new Icon(path_icon); n.Visible = true; n.Click += new EventHandler((object sender, EventArgs e) => { if(WindowState == FormWindowState.Minimized) { WindowState = FormWindowState.Normal; ShowInTaskbar = true; } else { WindowState = FormWindowState.Minimized; ShowInTaskbar = false; } }); load_files(); } private void load_files() { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; for (int i = 1; i <= 3; i++) { StreamReader sr = new StreamReader(Application.StartupPath + @"\dat" + i.ToString() +".txt", Encoding.GetEncoding("shift_jis")); try { string buff = ""; while (sr.Peek() > -1) { buff += sr.ReadLine() + Environment.NewLine; } if (i == 1) textBox1.Text = buff; if (i == 2) textBox2.Text = buff; if (i == 3) textBox3.Text = buff; } finally { sr.Close(); } } set_timer(); } Timer t; private void set_timer() { if (t != null) { t.Dispose(); t = null; } else { textBox4.Text = ""; label5.Text = DateTime.Now.ToString("yy/MM/dd hh:mm:ss"); init(); } t = new Timer(); //t.Enabled = true; t.Interval = int.Parse(textBox3.Text) * 1000; t.Tick += new EventHandler((object sender, EventArgs e) => { textBox4.Text = ""; label5.Text = DateTime.Now.ToString("yy/MM/dd hh:mm:ss"); init(); }); t.Start(); } bool flg = false; private void init() { StringReader sr_files = new StringReader(textBox2.Text); StringReader sr_words = new StringReader(textBox1.Text); List<string> list_files = new List<string>(); List<string> list_words = new List<string>(); try { // Peek()は読み取り対象の次の文字を返す。存在しないと -1 を返す。 while (sr_files.Peek() > -1) { list_files.Add(sr_files.ReadLine()); } while (sr_words.Peek() > -1) { list_words.Add(sr_words.ReadLine()); } foreach(string f in list_files) { foreach(string w in list_words) { chk(f, w); } } } finally { sr_words.Close(); sr_files.Close(); } if (flg) { n.BalloonTipText = "LOG CHECK"; n.ShowBalloonTip(10000); flg = false; } } private string enc(string str) { Encoding src = Encoding.ASCII; Encoding dst = Encoding.GetEncoding("shift_jis"); byte[] tmp = src.GetBytes(str); byte[] sjis_tmp = Encoding.Convert(src,dst,tmp); string sjis_str = dst.GetString(sjis_tmp); return sjis_str; } private void chk(string f, string w) { StreamReader sr = null; //f = enc(f); try { sr = new StreamReader(f, Encoding.GetEncoding("shift_jis")); int r = 1; while (sr.Peek() > -1) { string ln = sr.ReadLine(); Match m = Regex.Match(ln, w); if (m.Success) { textBox4.Text += r.ToString() + " " + ln + " (" + f + ")" + Environment.NewLine; flg = true; } r++; } } catch(UnauthorizedAccessException e) { MessageBox.Show(e.Message); return; } finally { if(sr != null) sr.Close(); } //sr.ReadToEndで読んで、Matchesを使うと一発。 //MatchCollection matched = Regex.Matches(buff, w); /* foreach (Match m in matched) { textBox4.Text += m.Value; } */ } private void button1_Click(object sender, EventArgs e) { for (int i = 1; i <= 3; i++) { StreamWriter sw = new StreamWriter(Application.StartupPath + @"\dat" + i.ToString() + ".txt", false, System.Text.Encoding.GetEncoding("shift_jis")); try { if (i == 1) sw.Write(textBox1.Text); if (i == 2) sw.Write(textBox2.Text); if (i == 3) sw.Write(textBox3.Text); } finally { sw.Close(); } } MessageBox.Show("登録しました。"); set_timer(); } private void button2_Click_1(object sender, EventArgs e) { if (t != null) t.Start(); } private void button3_Click(object sender, EventArgs e) { if (t != null) t.Stop(); } } } //非ジェネリック版コレクションは使わない。 //ArrayList は List<T> が正しい。 //ジェネリック = 型をパラメータで渡す。 //ArrayListはObjectにアップキャスト //List<T>は型を指定してインスタンスを作る。 |