System.Management won't run in C# Windows app after installation

112 Views Asked by At

I'm trying to use System.Management (specifically this line ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserAccount");) in a .NET 7 app. When I run the app in the debugger, everything is fine, and when I publish the app to a folder I can run the .exe file just fine.

The issue comes when I try to create an installer for the app. I've been using Microsoft Visual Studio Installer Extension to create a .msi file which buids fine with no warnings. However whenever I finish installing and try to run the app, it doesn't open, and when I check the Event Viewer I get this error

Application: Foo.exe
CoreCLR Version: 7.0.1523.57226
.NET Version: 7.0.15
Description: The process was terminated due to an unhandled exception.
Exception Info: System.PlatformNotSupportedException: System.Management currently is only supported for Windows desktop applications.
   at System.Management.ManagementObjectSearcher..ctor(String queryString)
   at DemoUI.HelperClasses.UserMonitor.ListUserAccounts() in C:\Users\user\Code\wireguard-custom\csharp\HelperClasses\UserMonitor.cs:line 35
   at DemoUI.LoginUI..ctor(LoginManager login_manager_in) in C:\Users\user\Code\wireguard-custom\csharp\DemoUI\LoginUI.cs:line 24
   at DemoUI.Program.Main(String[] args) in C:\Users\user\Code\wireguard-custom\csharp\DemoUI\Program.cs:line 57

For the life of me I can't figure out how to fix this. I have .NET 7 Runtime installed, and my app should be set to be a Windows Desktop app (although when I first initialized the project it wasn't one)

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <TargetFramework>net7.0-windows10.0.22621.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <Platforms>x64</Platforms>
    <DebugType>embedded</DebugType>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <ApplicationManifest>DemoUI\app.manifest</ApplicationManifest>
    <StartupObject>DemoUI.Program</StartupObject>
    <PlatformTarget>x64</PlatformTarget>
    <Title>Foo Windows App</Title>
    <PackageIcon>logo.png</PackageIcon>
    <RepositoryType>git</RepositoryType>
    <NeutralLanguage>en-US</NeutralLanguage>
    <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
    <UseWPF>true</UseWPF>
    <SupportedOSPlatformVersion>8.0</SupportedOSPlatformVersion>
    <ApplicationIcon>logo.ico</ApplicationIcon>
    <OutputType>WinExe</OutputType>
    <IsPublishable>False</IsPublishable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <WarningLevel>7</WarningLevel>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <WarningLevel>7</WarningLevel>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="wireguard.dll" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="logo.ico" />
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Include="tunnel.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </EmbeddedResource>
    <EmbeddedResource Include="wireguard.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </EmbeddedResource>
  </ItemGroup>

  <ItemGroup>
    <None Include="..\logo.png">
      <Pack>True</Pack>
      <PackagePath>\</PackagePath>
    </None>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="3.25.2" />
    <PackageReference Include="Grpc.Core" Version="2.46.6">
      <TreatAsUsed>true</TreatAsUsed>
    </PackageReference>
    <PackageReference Include="Grpc.Net.Client" Version="2.60.0" />
    <PackageReference Include="Grpc.Tools" Version="2.61.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="System.Management" Version="8.0.0" />
    <Protobuf Include="HelperClasses\Protos\connection.proto" GrpcServices="Client" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
  </ItemGroup>

</Project>

I can't figure out what is wrong. This is what my Visual Studio installer setup looks like, and I can see the System.Management.dll among the outputs

Visual Studio Installer Extension Setup View

And I get the same issue on Windows 11, or if I add a bunch of other outputs to the Setup. I'm really at my wits end and I would appreciate any insights anyone has on the subject. Thanks!

1

There are 1 best solutions below

0
haxonek On

So I could not find an actual solution. It doesn't look like it's a versioning issue though, since when I checked nuget.org all of the correct versions of libraries were downloaded for System.Management.

What I did find was by using inno, I followed the setup wizard and it worked perfectly, including some more complex logic which I couldn't easily do on visual studios installer. If anyone comes up with a solution though I'm happy to try and endorse your answer, however for now the inno installer works much better for me and doesn't have this issue.