Exception in GetInstances() method from ManagementClass in System.Management library

  • Thread starter Thread starter Balaji Renganathan
  • Start date Start date
B

Balaji Renganathan

Guest
Hi Support,

We are using ManagementClass from "System.Management.dll" assembly to find the drive is locked by bit-locker. Below is the code snippet we were using.

This is code works fine in Windows 10, 8 and lower versions

However, we are getting exception in "management.GetInstances();" in Windows Server 2012, 2016 and in some Windows 10 OS machines.

Is there any ways to resolve this exception, seems this is weird and occurs only on server os and some Windows 10

Code snippet:

var drive = new ManagementPath();
drive.NamespacePath = "\\ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption";
drive.ClassName = "Win32_EncryptableVolume";

var scope = new ManagementScope(drive, new ConnectionOptions() { Impersonation = ImpersonationLevel.Impersonate });
var management = new ManagementClass(scope, drive, new ObjectGetOptions());

var instances= management.GetInstances();

Exception Details:

ErrorCode: InvalidNamespace

Error Message: Invalid namespace

Stack Trace:

at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementScope.InitializeGuts(Object o)
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementObject.Initialize(Boolean getObject)
at System.Management.ManagementClass.GetInstances(EnumerationOptions options)
at ConsoleApplication1.Program.Main(String[] args) in C:\Users\Administrator\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 45
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Regards,
Balaji R


-ji

Continue reading...
 
Back
Top