Advertisement
mosmondor

RunOnIpChange

Mar 3rd, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Threading;
  6.  
  7. namespace RunOnIpChange
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string myLastIp = GetMyIp();
  14.             while (true)
  15.             {
  16.                 Thread.Sleep(15000);
  17.                 string myNewIp = GetMyIp();
  18.                 if (myNewIp != myLastIp)
  19.                 {
  20.                     Console.Write(string.Format("{0}: new IP: {1}", DateTime.Now, myNewIp));
  21.                     myLastIp = myNewIp;
  22.                     DoStuff();
  23.                 }
  24.                 else
  25.                 {
  26.                     Console.Write(string.Format("{0}: IP is still the same: {1}", DateTime.Now, myNewIp));
  27.                 }
  28.             }
  29.         }
  30.  
  31.         private static void DoStuff()
  32.         {
  33.         }
  34.  
  35.         private static string GetMyIp()
  36.         {
  37.             WebClient wc = new WebClient();
  38.             return wc.DownloadString("http://myexternalip.com/raw");
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement