我使用以下代码来指示代码是否在虚拟机上执行。
using (var searcher = new ManagementObjectSearcher("Select * from Win32_ComputerSystem"))
{
using (var items = searcher.Get())
{
foreach (var item in items)
{
string manufacturer = item["Manufacturer"].ToString().ToLower();
Console.WriteLine("Manufacturer: " + manufacturer);
Console.WriteLine("Model: " + item["Model"].ToString());
if ((manufacturer == "microsoft corporation" && item["Model"].ToString().ToUpperInvariant().Contains("VIRTUAL"))
|| manufacturer.Contains("vmware")
|| item["Model"].ToString() == "VirtualBox")
{
Console.WriteLine("Manufacturer: " + manufacturer);
Console.WriteLine("Model: " + item["Model"].ToString());
}
}
}
}
Console.Read();
但除此之外,我必须知道它是并行还是VnWare。任何的想法? 谢谢
根据提供的答案,最终代码将是这样的,它对我有用:
using (var searcher = new ManagementObjectSearcher("Select * from Win32_ComputerSystem"))
{
//TODO: Have to verify the difference between Parallel and Vm
using (var items = searcher.Get())
{
foreach (var item in items)
{
string manufacturer = item["Manufacturer"].ToString().ToLower();
string model = item["Model"].ToString().ToLower();
if (manufacturer.Contains("parallels") && model.Contains("parallels"))
{
Console.WriteLine("Parallels Detected");
}
else if(manufacturer.Contains("vmware"))
{
Console.WriteLine("VMWARE Detected");
}
}
}
}
答案 0 :(得分:1)
在VMware Fusion上运行此产品:
制造商:vmware,inc。
型号:VMware Virtual Platform
基于this,您可以安全地检查制造商或型号是否包含字符串“parallels”以确定它是否在并行运行。