EnderAlice

Kill unsupported OS test

Mar 28th, 2014
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public sealed class OsVersion
  2. {
  3.     /// <summary>
  4.     /// 太平洋標準時
  5.     /// </summary>
  6.     private static readonly TimeSpan PST = new TimeSpan(-8, 0, 0);
  7.     /// <summary>
  8.     /// Windows NT 5.1(Windows XP)のサポート終了日
  9.     /// </summary>
  10.     private static readonly DateTimeOffset WindowsNT_5_1_End = new DateTimeOffset(2014, 4, 8, 0, 0, 0, PST);
  11.     /// <summary>
  12.     /// Windows NT 5.2(Windows Server 2003, Windows XP x64 Edition)のサポート終了日
  13.     /// </summary>
  14.     private static readonly DateTimeOffset WindowsNT_5_2_End = new DateTimeOffset(2015, 7, 14, 0, 0, 0, PST);
  15.  
  16.     private delegate void KillOldVersionDelegate();
  17.     private static KillOldVersionDelegate Delegate;
  18.     private static IAsyncResult Result;
  19.     private static Boolean TerminateThread;
  20.     private static readonly String SystemDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
  21.     private static readonly FileVersionInfo KernelVersion = FileVersionInfo.GetVersionInfo(SystemDirectory + @"\NTOSKRNL.EXE");
  22.     private static readonly FileVersionInfo HardwareAbstractLayerVersion = FileVersionInfo.GetVersionInfo(SystemDirectory + @"\HAL.DLL");
  23.  
  24.     public static void KillOldVersion()
  25.     {
  26.         Delegate = new KillOldVersionDelegate(KillOldVersionThread);
  27.         TerminateThread = false;
  28.         Result = Delegate.BeginInvoke(null, null);
  29.     }
  30.  
  31.     public static void Finish()
  32.     {
  33.         TerminateThread = true;
  34.         Delegate.EndInvoke(Result);
  35.     }
  36.  
  37.     private static void KillOldVersionThread()
  38.     {
  39.         while(!TerminateThread)
  40.         {
  41.             if(!IsOperatingSystemSupportingNow())
  42.             {
  43.                 MessageBox.Show("現在実行中のオペレーティング システムはサポート期間を過ぎているため、動作を継続できません。", "致命的なエラー", MessageBoxButton.OK, MessageBoxImage.Hand);
  44.  
  45.                 // 終了処理
  46.  
  47.                 throw new Exception("Your operating system is not supported.");
  48.             }
  49.  
  50.             Thread.Sleep(1000);
  51.         }
  52.     }
  53.  
  54.     private static Boolean IsOperatingSystemSupportingNow()
  55.     {
  56.         var Now = new DateTimeOffset(DateTime.Now);
  57.         Int32 RequiredMajor, RequiredMinor, CurrentMajor, CurrentMinor;
  58.  
  59.         if(Now >= WindowsNT_5_2_End)
  60.         {
  61.             RequiredMajor = 6;
  62.             RequiredMinor = 0;
  63.         }
  64.         else if(Now >= WindowsNT_5_1_End)
  65.         {
  66.             RequiredMajor = 5;
  67.             RequiredMinor = 2;
  68.         }
  69.         else
  70.         {
  71.             RequiredMajor = 5;
  72.             RequiredMinor = 1;
  73.         }
  74.  
  75.         CurrentMajor = Math.Min(KernelVersion.ProductMajorPart, HardwareAbstractLayerVersion.ProductMajorPart);
  76.         CurrentMinor = Math.Min(KernelVersion.ProductMinorPart, HardwareAbstractLayerVersion.ProductMinorPart);
  77.  
  78.         if(CurrentMajor < RequiredMajor)
  79.         {
  80.             return false;
  81.         }
  82.  
  83.         if(CurrentMajor == RequiredMajor)
  84.         {
  85.             if(CurrentMinor < RequiredMinor)
  86.             {
  87.                 return false;
  88.             }
  89.         }
  90.  
  91.         return true;
  92.     }
  93. }
RAW Paste Data