4

我有以下 .csx 脚本:

#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");

当我使用它运行时,dotnet script我收到此错误:

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

如果我在 Visual Studio 中编译以下代码,一切正常:

using System.Management;
class Script {
    static void Main() {
        ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");
    }
}

为什么 ?我怎样才能让我的 .csx 脚本工作?

4

1 回答 1

0

dotnet 脚本在 .NET Core 上运行,我认为 WMI 不能在 .NET Core 中运行,因为它需要 COM 互操作。看到这个

此外,.NET CORE 的全局运行时 dll 不在 GAC 中,而是在运行时包存储中。

于 2021-10-25T04:21:19.243 回答