cs
using System;
using System.Management;
namespace DetectMacOSVM
{
class Program
{
static void Main(string[] args)
{
if (IsRunningInMacOSVM())
{
Console.WriteLine("This Windows system is running in a macOS virtual machine.");
}
else
{
Console.WriteLine("This Windows system is not running in a macOS virtual machine.");
}
}
static bool IsRunningInMacOSVM()
{
try
{
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"))
{
foreach (var obj in searcher.Get())
{
string manufacturer = obj["Manufacturer"]?.ToString() ?? string.Empty;
string model = obj["Model"]?.ToString() ?? string.Empty;
// Check for macOS-related VM indicators
if (manufacturer.Contains("Apple") || model.Contains("Mac"))
{
return true;
}
}
}
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"))
{
foreach (var obj in searcher.Get())
{
string biosVendor = obj["Manufacturer"]?.ToString() ?? string.Empty;
// Check for Apple-related BIOS information
if (biosVendor.Contains("Apple"))
{
return true;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
return false;
}
}
}
说明
- 通过 WMI 检索系统信息:
使用 Win32_ComputerSystem 检查制造商和模型名称是否包含 "Apple" 或 "Mac"。
使用 Win32_BIOS 检查 BIOS 信息是否与 Apple 相关。
- 虚拟机特征:
如果虚拟机运行在 macOS 主机上,虚拟机可能会暴露 Apple 的信息,比如制造商或 BIOS。
- 限制与注意事项:
这种方法依赖于虚拟机是否将 macOS 的相关信息暴露给 Windows。
某些虚拟机管理程序(如 VMware 或 Parallels)可能隐藏或自定义这些信息,导致检查失败。
- 真实场景中的变通:
可以结合网卡信息、硬件特性进一步判断(如虚拟化软件特定标识:Parallels、VMware 等)。
不支持在Window虚拟机中运行
cs
using System;
using System.Management;
namespace VirtualMachineDetector
{
class Program
{
static void Main(string[] args)
{
if (IsVirtualMachine())
{
Console.WriteLine("The current system is running in a virtual machine.");
}
else
{
Console.WriteLine("The current system is not running in a virtual machine.");
}
}
static bool IsVirtualMachine()
{
try
{
// Check for known virtual machine identifiers in Win32_ComputerSystem
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"))
{
foreach (var obj in searcher.Get())
{
string manufacturer = obj["Manufacturer"]?.ToString() ?? string.Empty;
string model = obj["Model"]?.ToString() ?? string.Empty;
// Common virtual machine manufacturers and models
if (manufacturer.ToLower().Contains("microsoft") && model.ToLower().Contains("virtual"))
return true;
if (manufacturer.ToLower().Contains("vmware"))
return true;
if (manufacturer.ToLower().Contains("xen"))
return true;
if (manufacturer.ToLower().Contains("qemu"))
return true;
if (manufacturer.ToLower().Contains("parallels"))
return true;
if (manufacturer.ToLower().Contains("oracle") && model.ToLower().Contains("virtualbox"))
return true;
}
}
// Check for BIOS information in Win32_BIOS
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"))
{
foreach (var obj in searcher.Get())
{
string biosVersion = obj["SMBIOSBIOSVersion"]?.ToString() ?? string.Empty;
string manufacturer = obj["Manufacturer"]?.ToString() ?? string.Empty;
// Check for virtual machine specific BIOS information
if (biosVersion.ToLower().Contains("vmware"))
return true;
if (biosVersion.ToLower().Contains("virtualbox"))
return true;
if (manufacturer.ToLower().Contains("xen"))
return true;
if (manufacturer.ToLower().Contains("parallels"))
return true;
}
}
// Check for processor information in Win32_Processor
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"))
{
foreach (var obj in searcher.Get())
{
string processorName = obj["Name"]?.ToString() ?? string.Empty;
// Virtual processors often have specific names
if (processorName.ToLower().Contains("virtual"))
return true;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while detecting virtual machine: {ex.Message}");
}
return false;
}
}
}