如何在 C# 中知道该程序在 Mac 运行 XP SP2 上的 Parallels 上是 运行 或 Mac 运行 Windows 上的 VmWare 7
How to know in C# that program is running on Parallels on a Mac running XP SP2 or VmWare on a Mac running Windows 7
我正在使用以下代码来指示代码是否在虚拟机上执行。
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();
但除此之外,我还必须知道它是 Parallel 还是 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");
}
}
}
}
运行 这在 VMware Fusion 上产生:
Manufacturer: vmware, inc.
Model: VMware Virtual Platform
根据 this,您似乎可以安全地检查 Manufacturer 或 Model 是否包含字符串 "parallels" 以确定它是否 运行 平行。
我正在使用以下代码来指示代码是否在虚拟机上执行。
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();
但除此之外,我还必须知道它是 Parallel 还是 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");
}
}
}
}
运行 这在 VMware Fusion 上产生:
Manufacturer: vmware, inc.
Model: VMware Virtual Platform
根据 this,您似乎可以安全地检查 Manufacturer 或 Model 是否包含字符串 "parallels" 以确定它是否 运行 平行。