I wish to add to my UWP application the ability for it to shut-down its local machine upon invocation by a button click, however, no method that I have tried thus far has worked. For instance:
I have tried to command Windows to shut-down, but the command that I utilised does not work specifically when invoked by my application.
shutdown /s /t 120
Therefore, I tried to use native C# code in order to do so. However, using this tells me that it does not possess the appropriate permissions to do so.
ShutdownManager.BeginShutdown(Windows.System.ShutdownKind.Shutdown, TimeSpan.FromSeconds(120));
I have tried many methods but I simply cannot seem to fix it.
Relevant files
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>
Please note that I wish to use this command on Windows 10, not Windows 10 IoT; I merely am trying to use those commands because they're the only other way I can think of doing this without relying upon either PowerShell, Python or the Windows Command Processor/Command Prompt.