public partial class Form1 : Form
{
WebClient webClient = new WebClient();
Timer Timer = new Timer();
public Form1()
{
InitializeComponent();
Timer.Interval = 1000 * 60 * 10; // 10分に1回調べる
Timer.Tick += Timer_Tick;
Timer.Start();
this.NotifyIcon1.Text = "IPアドレス監視中";
this.NotifyIcon1.Visible = true;
this.NotifyIcon1.MouseClick += this.NotifyIcon1_MouseClick;
CheckIP();
}
void CheckIP()
{
string nowIP = webClient.DownloadString("https://lets-csharp.com/sample/get-ip/get-ip.php");
string path = Application.StartupPath + "\\ip.txt";
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
string oldIP = sr.ReadToEnd();
sr.Close();
if (oldIP != nowIP)
{
StreamWriter sw = new StreamWriter(path);
sw.Write(nowIP);
sw.Close();
string str = String.Format("{0} => {1}", oldIP, nowIP);
MessageBox.Show(str, "IPアドレスが変更されています");
}
}
else
{
StreamWriter sw = new StreamWriter(path);
sw.Write(nowIP);
sw.Close();
}
label1.Text = nowIP;
}
private void Timer_Tick(object sender, EventArgs e)
{
CheckIP();
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.Visible = true;
}
bool isEnd = false;
protected override void OnClosing(CancelEventArgs e)
{
if (!isEnd)
{
this.Visible = false;
e.Cancel = true;
}
base.OnClosing(e);
}
}