動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2
処理概要
確認ダイアログを表示して、shutdown 処理を行う。
参考
https://dobon.net/vb/dotnet/system/shutdown.html#section4
の「shutdown.exeで行う」を利用しました。
(var利用に一部変更)。
shutdown
code
MainWindow.xaml
<Window x:Class="_171110_t1305_shutdown.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_171110_t1305_shutdown"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Shutdown" Height="30" Width="100" Click="Button_Click"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _171110_t1305_shutdown
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PowerProcess()
{
var psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "shutdown.exe";
psi.Arguments = "/s";
// ウィンドウ非表示
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
// 起動
var proc = System.Diagnostics.Process.Start(psi);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// 1. 確認ダイアログ
string cnfmsg = "";
cnfmsg = "シャットダウン処理を続けますか?";
MessageBoxResult res = MessageBox.Show(cnfmsg, "Confirmation", MessageBoxButton.OKCancel,
MessageBoxImage.Question, MessageBoxResult.Cancel);
if (res == MessageBoxResult.Cancel)
{
return;
}
// 2. shutdown or reboot
PowerProcess();
}
}
}
reboot
rebootするには上記のコードの一部を以下とする。
psi.Arguments = "/r";
検索用キーワード
- シャットダウン
- 再起動