I have the following .csx script:
#r "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Management.dll"
using System.Management;
ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");
When I run this using dotnet script
I get this error:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Management.MTAHelper.IsNoContextMTA()
at System.Management.MTAHelper.CreateInMTA(Type type)
at System.Management.ManagementPath.CreateWbemPath(String path)
at System.Management.ManagementPath..ctor(String path)
at System.Management.ManagementClass..ctor(String path)
at Submission#0.<<Initialize>>d__0.MoveNext() in <..path..>\script.csx:line 3
--- End of stack trace from previous location ---
at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in <...path...>\Temp\tmpBFB\Dotnet.Script.Core\ScriptRunner.cs:line 110
If I compile the following code in visual studio, everything works fine:
using System.Management;
class Script {
static void Main() {
ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");
}
}
Why ? How can I get my .csx script to work ?
dotnet script runs off .NET Core and I don't think WMI works in .NET Core as it needs COM interop. See this.
Also, global runtime dll for .NET CORE is not in GAC but rather in a runtime package store.