You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
using statement does not work with ManagementBaseObject or ManagementObject #117539
Using statements do not properly dispose of System.Management.ManagementBaseObject or System.Management.ManagementObject due to both classes defining new Dispose() methods that hide the base class method with new rather than overriding the virtual void Dispose(bool disposing) from System.ComponentModel.Component.
Reproduction Steps
usingSystem.Management;using(varmo=newManagementObject()){}// step through with a debugger and observe that neither ManagementObject.Dispose() nor ManagementBaseObject.Dispose() are called, but only Componnet.Dispose()
Expected behavior
ManagementObject and ManagementBaseObject have disposal code called.
Actual behavior
Only Component has its disposal code called.
Regression?
No
Known Workarounds
Avoid using using statements with these classes and instead use a try/finally.
This is because ManagementBaseObject or ManagementObject doesn't specify IDisposable implementation, thus new Dispose isn't marked as interface implementation.
Not clear why this is the case, but it's been that way since .NET 2.0. I couldn't find anything in the internal source history to indicate why. System.Management is in maintenance mode so we're unlikely to change this.
At one point that was supposed to be Microsoft Management Infrastructure - but not sure any more. This portion of Windows is owned by the Powershell folks - you can ask that question over in https://github.com/powershell/powershell. They may reccomend that folks use platform API rather than WMI.
Thank you, @ericstj. There is this discussion in the PowerShell repo open since January with no response. Any suggestions for bringing attention to it / getting a response?
Thank you, @ericstj. There is this discussion in the PowerShell repo open since January with no response. Any suggestions for bringing attention to it / getting a response?
To add insult to injury the repo is archived... so I really can't know what library to use...
Activity
dotnet-policy-service commentedon Jul 11, 2025
Tagging subscribers to this area: @dotnet/area-system-management
See info in area-owners.md if you want to be subscribed.
huoyaoyuan commentedon Jul 11, 2025
This is because
ManagementBaseObjectorManagementObjectdoesn't specifyIDisposableimplementation, thusnew Disposeisn't marked as interface implementation.Example:
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7AHwAEAmARgFgAoaogBgAIAKPGAdwYBEymBKX6gG9qDUQ2oBfarUYt2XEnwFVhVMeKpSaVIgGYGpBgGEyDEAwCSnAJYBnAA4RbAQ2AAbGEJFi9BlFztHWxg+b1FVdXUiMgBOJgAiE3jeAG4whi0tWn1DbjNjShV031YOIn8bBycQ5XUIyNFouPjuZLS1MUzpbV9DIxJ8q0CnVw8vDsaciuHg0ImGeoamhP629K6enIHOAfN+tEtKoNHPIvmS+XKAqtnasUXI5ZaSNfnMoA=
ericstj commentedon Jul 14, 2025
Not clear why this is the case, but it's been that way since .NET 2.0. I couldn't find anything in the internal source history to indicate why. System.Management is in maintenance mode so we're unlikely to change this.
cyungmann commentedon Jul 14, 2025
@ericstj is there a replacement for performing WMI queries from C# code?
ericstj commentedon Jul 14, 2025
At one point that was supposed to be Microsoft Management Infrastructure - but not sure any more. This portion of Windows is owned by the Powershell folks - you can ask that question over in https://github.com/powershell/powershell. They may reccomend that folks use platform API rather than WMI.
cyungmann commentedon Jul 17, 2025
Thank you, @ericstj. There is this discussion in the PowerShell repo open since January with no response. Any suggestions for bringing attention to it / getting a response?
loudenvier commentedon Sep 22, 2025
To add insult to injury the repo is archived... so I really can't know what library to use...