C# Windows系统一键关机源代码共享

C# Windows系统一键关机源代码,可移植(复制粘贴)到的自己的项目中

exe可执行文件下载

C# 源码下载

csharp 复制代码
using System;
using System.Windows;
using System.Windows.Threading;

namespace 关机
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 关机定时
        /// </summary>
        private DispatcherTimer shutdown;

        public MainWindow()
        {
            InitializeComponent();

            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            shutdown = new DispatcherTimer();
            shutdown.Tick += new EventHandler(ShowCurTimer);
            shutdown.Interval = new TimeSpan(0, 0, 0, 1, 0);
            shutdown.Start();
            try{ 
            }
            catch (Exception){
                //this.Dispatcher.Invoke(new Action(delegate { this.Close(); }));
            }
        }

        #region 执行关机
        public void ShowCurTimer(object sender, EventArgs e){
            关机();
        }
        private void 关机(){
            System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo();
            ps.FileName = "shutdown.exe";
            ps.Arguments = "-s -t 1"; System.Diagnostics.Process.Start(ps);
        }
        #endregion

    }
}

布局

csharp 复制代码
<Window x:Class="关机.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:关机"
        mc:Ignorable="d"
        Title="MainWindow" Height="108" Width="377" WindowStyle="None" AllowsTransparency="True" ShowInTaskbar="True" Background="{x:Null}" >
    <Grid>
        <Grid.Background>
            <ImageBrush/>
        </Grid.Background>
        <!--时间-->
        <Label Content="正在关机。。。" FontSize="35" Foreground="#FF02F3FF" HorizontalAlignment="Left" Margin="78,24,0,0" VerticalAlignment="Top" Height="74" Width="249"/>
    </Grid>
</Window>
相关推荐
.m1 小时前
无法打开windows安全中心解决方案
windows
Top`1 小时前
Java 泛型 (Generics)
java·开发语言·windows
九流下半1 小时前
window wsl 环境下编译openharmony,HarmonyOS 三方库 FFmpeg
windows·ffmpeg·harmonyos·编译·openharmony·三方库
brave_zhao4 小时前
JavaBeanUtils javaBean转map, 实体类转map,实体集合转List<Map>
linux·windows·python
CodeCraft Studio5 小时前
PPT处理控件Aspose.Slides教程:使用 C# 将 PPTX 转换为 EMF
c#·powerpoint·ppt·aspose·ppt格式转换
future14126 小时前
游戏开发日记7.12
数据结构·学习·c#·游戏开发
洁辉6 小时前
C# & .NET 面试深度复习指南
面试·c#·.net
_oP_i8 小时前
无法找到来自源 EdgeWebView,实际安装了,偶尔出现
c#
阿蒙Amon9 小时前
C#类型转换:从基础到进阶的全景解析
开发语言·c#