Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Management;
- using System.Text.RegularExpressions;
- namespace yournamespace
- {
- class sysinfo
- {
- public static Boolean isVM()
- {
- bool foundMatch = false;
- ManagementObjectSearcher search1 = new ManagementObjectSearcher("select * from Win32_BIOS");
- var enu = search1.Get().GetEnumerator();
- if (!enu.MoveNext()) throw new Exception("Unexpected WMI query failure");
- string biosVersion = enu.Current["version"].ToString();
- string biosSerialNumber = enu.Current["SerialNumber"].ToString();
- try
- {
- foundMatch = Regex.IsMatch(biosVersion + " " + biosSerialNumber, "VMware|VIRTUAL|A M I|Xen", RegexOptions.IgnoreCase);
- }
- catch (ArgumentException)
- {
- // Syntax error in the regular expression
- }
- ManagementObjectSearcher search2 = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
- var enu2 = search2.Get().GetEnumerator();
- if (!enu2.MoveNext()) throw new Exception("Unexpected WMI query failure");
- string manufacturer = enu2.Current["manufacturer"].ToString();
- string model = enu2.Current["model"].ToString();
- try
- {
- foundMatch = Regex.IsMatch(manufacturer + " " + model, "Microsoft|VMWare|Virtual", RegexOptions.IgnoreCase);
- }
- catch (ArgumentException)
- {
- // Syntax error in the regular expression
- }
- return foundMatch;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment