Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 Processor Id, Disk Volume number etc.

like image 344
user2449952 Avatar asked Nov 12 '16 20:11

user2449952


People also ask

How do I add a DLL reference in Visual Studio NET Core?

One of the things I did miss was a clear direction on how to reference a . NET dll directly (and of course still enjoy the benefits of intellisense), basically the same thing as doing Project > Add Reference > Browse in Visual Studio. And that's it!

How do I add a reference to System management DLL?

Right click on the references in your project. Click Add Reference. Select the file (Project Root > Bin > System. Management.

Where is System management automation DLL?

dll from C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System. Management.

Can Mono run .NET Core?

NET Core is supported on Mono (or if it even needs mono, now?), at least not entirely. Take a look here for what Mono supports. FastCGI is simply the server that runs ASP.NET code with mono. Now, having said that, is there a particular reason you need to run it on Linux?


2 Answers

.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.

like image 111
Bob Bryan Avatar answered Sep 28 '22 10:09

Bob Bryan


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.

like image 21
lulas Avatar answered Sep 28 '22 12:09

lulas