Guest User

sysinfo

a guest
Jun 10th, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Management;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace yournamespace
  6. {
  7.     class sysinfo
  8.     {
  9.         public static Boolean isVM()
  10.         {
  11.             bool foundMatch = false;
  12.             ManagementObjectSearcher search1 = new ManagementObjectSearcher("select * from Win32_BIOS");
  13.             var enu = search1.Get().GetEnumerator();
  14.             if (!enu.MoveNext()) throw new Exception("Unexpected WMI query failure");
  15.             string biosVersion = enu.Current["version"].ToString();
  16.             string biosSerialNumber = enu.Current["SerialNumber"].ToString();
  17.  
  18.             try
  19.             {
  20.                 foundMatch = Regex.IsMatch(biosVersion + " " + biosSerialNumber, "VMware|VIRTUAL|A M I|Xen", RegexOptions.IgnoreCase);
  21.             }
  22.             catch (ArgumentException)
  23.             {
  24.                 // Syntax error in the regular expression
  25.             }
  26.  
  27.             ManagementObjectSearcher search2 = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
  28.             var enu2 = search2.Get().GetEnumerator();
  29.             if (!enu2.MoveNext()) throw new Exception("Unexpected WMI query failure");
  30.             string manufacturer = enu2.Current["manufacturer"].ToString();
  31.             string model = enu2.Current["model"].ToString();
  32.             try
  33.             {
  34.                 foundMatch = Regex.IsMatch(manufacturer + " " + model, "Microsoft|VMWare|Virtual", RegexOptions.IgnoreCase);
  35.             }
  36.             catch (ArgumentException)
  37.             {
  38.                 // Syntax error in the regular expression
  39.             }
  40.  
  41.             return foundMatch;
  42.         }
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment