How to detect if my application is running in a virtual machine?

前端 未结 10 1260
青春惊慌失措
青春惊慌失措 2020-11-28 05:52

How can I detect (.NET or Win32) if my application is running in a virtual machine?

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 05:58

    Jay Abuzi showed the solution in powershell. Here's the same as a c# function:

       /// 
        /// Detect if this OS runs in a virtual machine
        /// 
        /// http://blogs.msdn.com/b/virtual_pc_guy/archive/2005/10/27/484479.aspx
        /// 
        /// Microsoft themselves say you can see that by looking at the motherboard via wmi
        /// 
        /// false if it runs on a fysical machine
        public bool DetectVirtualMachine()
        {
            bool result = false;
          const  string  MICROSOFTCORPORATION ="microsoft corporation";
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_BaseBoard");
    
                foreach (ManagementObject queryObj in searcher.Get())
                {
                   result =  queryObj["Manufacturer"].ToString().ToLower() == MICROSOFTCORPORATION.ToLower();
                }
                return result;
            }
            catch (ManagementException ex)
            {
                return result;
            }
        }
    

提交回复
热议问题