Is it possible using C# to check if hardware virtualization enabled?

a 夏天 提交于 2019-12-23 01:42:34

问题


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();
}

回答1:


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();



回答2:


Just an FYI, "root/virtualization" namespace no longer exists, starting with Windows / Hyper-V Server 2012 R2 (possibly Windows 8.1 too). Also, it might be because you wrote "root\virtualization" (try forward slashes). You should be using "root/virtualization/v2" anyways.



来源:https://stackoverflow.com/questions/24841536/is-it-possible-using-c-sharp-to-check-if-hardware-virtualization-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!