Missing reference to System.Diagnostics.Process

2019-01-20 17:31发布

问题:

I have a app that I am building to shut down a computer

I tried using using System.Management; but it tells me to add a reference, No such thing in the add reference list I tried using this code but the proccess has a red squigly

System.Diagnostics.Process.Start("Shutdown", "-s -t 10");

Any ideas on how to shut down?

回答1:

There's one simple explanation for a mystifying problem like this: you are using the Portable Class Libraries or have selected the ".NET for Metro style apps" platform target. Which does not permit using the Process class, Metro apps operate in a sandbox that disables many standard .NET features. Starting another process or shutting down the OS is not permitted, only the user can do that.

You'll have to give up on Metro if this is important to you, your app needs to run as a desktop app. And won't run on a slate that boots Win8.



回答2:

I ended up

  1. upgrading to the latest Visual Studio 2015 SP3,
  2. running Scott Smith's upgrade command using powershell,

    powershell dnvm upgrade -r coreclr
    

    https://stackoverflow.com/a/34013990/80772

  3. creating a console project anew from Visual Studio.

After adding more packages required to resolve the standard classes I observed this global.json,

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003131"
  }
}

and this project.json,

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    },
    "system.diagnostics.textwritertracelistener": "4.0.0-beta-23123",
    "System.Diagnostics.TraceSource": "4.0.0",
    "System.IO": "4.1.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}