简体   繁体   中英

UnauthorizedAccessException when using ShutdownManager even with systemManagement declared

Note: not duplicate of this as I already declare systemManagement capacity.

UPDATE : the API is only available to IoT devices, if you want an API for UWP, vote it here .

I have a simple UWP app that has both Target and Min version 2004 (Build 19041) . The UI simply has two buttons Shutdown and Restart:

private void OnShutDownClicked(object sender, RoutedEventArgs e)
{
    ShutdownManager.BeginShutdown(ShutdownKind.Shutdown, TimeSpan.FromSeconds(1));
}

private void OnRestartCLicked(object sender, RoutedEventArgs e)
{
    ShutdownManager.BeginShutdown(ShutdownKind.Restart, TimeSpan.Zero);
}

Following the doc , I added systemManagement capacity into the manifest using the UI, and also double-checked the XML code:

在此处输入图像描述

<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">
  ...
  <Capabilities>
    <iot:Capability Name="systemManagement"/>
  </Capabilities>
  ...

However, I still get UnauthorizedAccessException when executing the code, no matter which code I run (Shutdown or Restart). I already tried uninstalling the app and load it again from VS.

System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'

在此处输入图像描述

Did I miss something? How do I resolve this?


UPDATE: so I tried adding this line to test if I actually have access to this capacity:

var temp = ShutdownManager.IsPowerStateSupported(PowerState.ConnectedStandby);

According to the doc , this method also requires the same capacity. However, the code runs well through it. That means something must be missing from BeginShutdown :

在此处输入图像描述


UPDATE 2: Apparently this API is for IoT devices only? Can I use this API on Windows Desktop device or is there any similar API? Google always lead me there and there doesn't seem to be any alternative.

This API is designed for IoT devices and the function is not supported in UWP apps. UWP apps are running in a sandbox that is isolated so UWP apps have limited access to system resources and are not allowed to access these system functions like shutting down the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM