Rob*_*los 39
这是我使用的:
using (var searcher = new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem"))
{
using (var items = searcher.Get())
{
foreach (var item in items)
{
string manufacturer = item["Manufacturer"].ToString().ToLower();
if ((manufacturer == "microsoft corporation" && item["Model"].ToString().ToUpperInvariant().Contains("VIRTUAL"))
|| manufacturer.Contains("vmware")
|| item["Model"].ToString() == "VirtualBox")
{
return true;
}
}
}
}
return false;
Run Code Online (Sandbox Code Playgroud)
编辑2014-12-02:更新了代码,以便它不再将Microsoft Surface Pro检测为VM.感谢Erik Funkenbusch指出这一点.
编辑2017-06-29:更新了代码,以便它还检查HypervisorPresent
属性的值.
编辑2018-02-05:删除了对HypervisorPresent
属性的检查,因为它不正确.如果在hyper-V服务器上的主机O/S上运行,则此属性可能返回true.
Jay*_*uzi 19
根据Virtual PC Guy的博客文章" 检测Microsoft虚拟机 ",您可以使用WMI检查主板的制造商.在PowerShell中:
(gwmi Win32_BaseBoard).Manufacturer -eq "Microsoft Corporation"
Run Code Online (Sandbox Code Playgroud)
Art*_*yan 12
以下是一种实现方法的示例.它只适用于微软的Virtual PC和VMWare,但它是一个开始:http: //www.codeproject.com/KB/system/VmDetect.aspx
小智 5
此 C 函数将检测 VM 来宾操作系统:(在 Windows 上测试,使用 Visual Studio 编译)
#include <intrin.h>
bool isGuestOSVM()
{
unsigned int cpuInfo[4];
__cpuid((int*)cpuInfo,1);
return ((cpuInfo[2] >> 31) & 1) == 1;
}
Run Code Online (Sandbox Code Playgroud)