且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有可能使用C#来检查是否启用硬件虚拟化?

更新时间:2022-10-23 16:12:54

如果您枚举所有属性Win32_Processor类,你会看到一个名为财产 VirtualizationFirmwareEnabled ,我认为你是在谈论这个属性。



正如你可以看到的此链接,检查是否一些机处理器能够在Hyper-V上运行,它们使用 VirtualizationFirmwareEnabled 结合其他性能:



我写了这个简单的文档片断遍历所有win32_processor类值:

  ManagementClass managClass =新ManagementClass(win32_processor); 
ManagementObjectCollection managCollec = managClass.GetInstances();

的foreach(的ManagementObject managObj在managCollec)
{
的foreach(在managObj.Properties VAR道具)
{
Console.WriteLine(物业名称:{0}值:{1},prop.Name,道具.value的);
}
}

Console.ReadKey();


Couldn't find an answer, so I thought I ask for myself.

Using C#, how can I check if the CPU has hardware virtualization enabled? Hyper-V, for example. It's not in the Win32_Processor class, and my attempt to connect to the MSVM_ComputerSystem was met with utter failure. Keeps telling me there's an invalid namespace.

Code below.

ManagementObjectSearcher vm = new ManagementObjectSearcher(@"\\.\root\virtualization", "select * from MSVM_Computersystem");
ManagementObjectCollection vmCollection = vm.Get();
foreach (ManagementObject mo in vmCollection)
{
     cpuVirtual = mo["EnabledState"].ToString();
}

if you enumerate all properties in Win32_Processor class, you see a property named "VirtualizationFirmwareEnabled", I think you are talking about this property.

As you can see in this link, to check if some machine processor can run on Hyper-V, they use VirtualizationFirmwareEnabled" in conjunction to other properties:

I wrote this simple snipped to iterate over all win32_processor class values:

ManagementClass managClass = new ManagementClass("win32_processor");
ManagementObjectCollection managCollec = managClass.GetInstances();

foreach (ManagementObject managObj in managCollec)
{
   foreach (var prop in managObj.Properties)
   {
       Console.WriteLine("Property Name: {0} Value: {1}",prop.Name,prop.Value);
   }               
}

Console.ReadKey();