我的UWP应用如何获得启动关机所需的权限?

问题描述

我希望在我的UWP应用程序中添加通过单击按钮即可在调用关闭其本地计算机的功能,但是,到目前为止,我还没有尝试过任何方法。例如:

我曾试图命令Windows关闭,但是当我的应用程序调用该命令时,我使用的命令并不起作用。

shutdown /s /t 120

因此,我尝试使用本机C#代码。但是,使用它告诉我它没有适当的权限。

ShutdownManager.BeginShutdown(Windows.System.ShutdownKind.Shutdown,TimeSpan.FromSeconds(120));

我尝试了很多方法,但是我似乎无法修复它。

相关文件

MainPage.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System;
using Windows.System;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409.
// Delete the extra namespaces when the application is complete.

namespace Shutdown_Roulette
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_click(object sender,RoutedEventArgs e)
        {
            ShutdownManager.BeginShutdown(Windows.System.ShutdownKind.Shutdown,TimeSpan.FromSeconds(120));
        }
    }
}

Package.appxmanifest

<?xml version="1.0" encoding="utf-8"?>

<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">

  <Identity
    Name="7eb73f1e-b159-4fd0-aab9-4158e57ba08a"
    Publisher="CN=rokeb"
    Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="7eb73f1e-b159-4fd0-aab9-4158e57ba08a" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

  <Properties>
    <displayName>Shutdown Roulette</displayName>
    <PublisherdisplayName>Master Roke Julian Lockhart Beedell</PublisherdisplayName>
    <logo>Assets\Storelogo.png</logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="Shutdown_Roulette.App">
      <uap:VisualElements
        displayName="Shutdown Roulette"
        Square150x150logo="Assets\Square150x150logo.png"
        Square44x44logo="Assets\Square44x44logo.png"
        Description="Shutdown Roulette"
        BackgroundColor="transparent">
        <uap:DefaultTile Wide310x150logo="Assets\Wide310x150logo.png" ShortName="Shutdown Roulette">
        </uap:DefaultTile >
        <uap:SplashScreen Image="Assets\SplashScreen.png" />
      </uap:VisualElements>
    </Application>
  </Applications>

  <Capabilities>
    <iot:Capability Name="systemmanagement"/></Capabilities>
</Package>

请注意,我希望在Windows 10而不是Windows 10 IoT上使用此命令;我只是尝试使用这些命令,因为它们是我可以想到的唯一不依赖PowerShell,Python或Windows命令处理器/命令提示符的方式。

解决方法

根据ShowdownManager的描述,它需要IOT SDK的支持,这意味着它是一个平台受限的API(仅在IOT设备上有效)。

在Windows 10桌面设备中,UWP没有关闭设备的权限。