Not able to use System.Management.dll in Dot Net Core

后端 未结 4 1352
孤街浪徒
孤街浪徒 2021-02-18 13:31

How should I gather Hardware Info if System.Management.dll is not compatible with Dot Net Core.

How do I get the Machine info like Processo

相关标签:
4条回答
  • 2021-02-18 14:04

    I found this issue while upgrading from .NetFramework to .NetCore 3.0

    .NetFramework compatible System.Management won't work for the dotnet core, so to fix the issue just add a .Net core compatible System.Management nuget.

    At this point the latest available version is 4.6.0 https://www.nuget.org/packages/System.Management/4.6.0 and latest preview is 4.7.0-preview3.19551.4

    0 讨论(0)
  • 2021-02-18 14:13

    .NET Core 3 now supports System.Management. As of this time, .NET Core 3 is in preview mode with preview 4 as the most current.

    You will have to select the Manage NuGet packages... menu item under the Project menu in Visual Studio to install the latest version of System.Management. Make sure that the include previews checkbox is selected so that you will install the latest version.

    The previous version of System.Management throws an error:

    "Cannot marshal a string by-value with the [Out] attribute..."

    See this link for more info about the error.

    0 讨论(0)
  • 2021-02-18 14:19

    I am currently porting a Net Framework project that uses System.Management to get some hardware info, and I also got to a dead end because System.Management is not compatible with Net Core.

    The workaround I found was to use Process.Start from System.Diagnostics to execute powershell commands to get that information, for instance, to get the motherboard serial number you can use the command Get-WmiObject Win32_BaseBoard | select SerialNumber, you can then change the command to process the output and only have the serial number, or even process the output programatically.

    You can also add some ifs, that depending on the current OS you can provide to the Process.Start specific OS commands, for instance, for linux you would need to process a dmidecode related command.

    The only negative side in this approach is that its A LOT SLOWER whem compared with System.Management.

    0 讨论(0)
  • 2021-02-18 14:24

    You need to install Microsoft.Windows.Compatibility in your .Net core project to be able to reference System.Management.dll.

    0 讨论(0)
提交回复
热议问题