I want to allow my UWP application to initiate a shutdown.
Please do note that I want to utilize this command on Windows 10, rather than Windows 10 IoT; I am merely attempting to utilize IoT's commands because they are the sole alternative method (I have been able to conceive) of invocation of this without reliance upon either PowerShell, Python, or the Windows Command Processor.
Attempting to invoke shell commands like Powershell's
Stop-Computer
doesn't work.
Consequently, I attempted to use native C# code. However, doing that causes the undermentioned error:
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://learn.microsoft.com/en-us/previous-versions/windows/apps/hh768232(v=win.10)?redirectedfrom=MSDN#project-templates-at-a-glance. // 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>
According to the description of ShowdownManager, it needs the support of IOT SDK, which means it is a platform-limited API (only valid on IOT devices).
In Windows 10 desktop devices, UWP does not have the permission to turn off the device.