You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8发布WinForms应用指定win-x64时WMI报错原因咨询

问题:WinForms发布目标切换为win-x64后调用WMI出现System.Management报错的原因

将WinForms应用的发布目标运行时从portable切换为win-x64后,调用WMI功能时出现运行时错误:

System.Management currently is only supported for Windows desktop applications
切回portable则问题消失。

项目相关配置如下:

  1. csproj文件配置:
<TargetFramework>net8.0-windows</TargetFramework>
<PackageReference Include="System.Management" Version="8.0.0" />
  1. 发布配置文件.pubxml内容:
<Project>
  <PropertyGroup>
    <Configuration>SQL01</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>\\192.168.4.14\public\November\Publish</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <TargetFramework>net8.0-windows</TargetFramework>
    <SelfContained>false</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>false</PublishSingleFile>
    <PublishReadyToRun>false</PublishReadyToRun>
  </PropertyGroup>
</Project>
  1. 示例WMI调用代码:
var mo = new ManagementObject
{
    Path = new ManagementPath(string.Format("Win32_LogicalDisk='{0}'", root))
};

原因分析

当指定win-x64作为RuntimeIdentifier(RID)发布非自包含的WinForms应用时,.NET 8的运行时环境会误判应用类型,不再将其识别为Windows桌面应用。而System.Management 8.0.0包的兼容性校验逻辑会严格检查应用类型,仅允许在明确标记为Windows桌面应用的环境中运行,因此触发了报错。

而使用portable模式(即不指定具体RID)发布时,运行时会默认以Windows桌面应用的上下文加载程序集,跳过了这个严格的类型校验,所以WMI功能可以正常工作。

尽管项目的目标框架设置为net8.0-windows,但指定具体RID后,发布流程中的应用类型识别逻辑会出现偏差,导致System.Management的兼容性检查失败。


内容的提问来源于stack exchange,提问作者IngoB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 03:12:50