Error code when trying to shutdown using UWP and C#

1

I want to invoke a shutdown if label1.text == label2.text but an error appears when Windows.System.ShutdownManager.BeginShutdown(Windows.System.ShutdownKind.Shutdown, TimeSpan.FromSeconds(1)); is called.

The error:

System.UnauthorizedAccessException : 'Accès refusé. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'

c#
uwp
asked on Stack Overflow May 12, 2018 by Notwin • edited Nov 13, 2020 by BEEDELL ROKE JULIAN LOCKHART

2 Answers

2
// Shutdowns the device within 1 second:
ShutdownManager.BeginShutdown(ShutdownKind.Shutdown, TimeSpan.FromSeconds(1));

In order for this code to work properly, you need to give an extra capability to your app, open the Package.appxmanifest file with Visual Studio XML Editor and add :

<Package
     ...
   <Capabilities>
    ...
     <iot:Capability Name="systemManagement" />
   </Capabilities>
</Package>

If you don't set this capability, you will get an UnauthorizedAccessException error when calling ShutdownManager methods

From Microsoft docs

This API requires the use of the IoT systemManagement capability. Users can add the following to their Package.appmanifest: <iot:Capability Name="systemManagement"/>

answered on Stack Overflow May 12, 2018 by Mugiwara • edited May 12, 2018 by Mugiwara
1

ShutdownManager API requires the use of the IoT systemManagement capability, and the inclusion of iot in the IgnorableNamespaces list. Users can add the following to their Package.appmanifest:, and add iot to their existing list of IgnorableNamespaces.

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
  IgnorableNamespaces="uap mp iot">
answered on Stack Overflow Jul 5, 2019 by Konstantin

User contributions licensed under CC BY-SA 3.0