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>は型を指定してインスタンスを作る。