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>
相关推荐
WebRuntime12 分钟前
问世间,exe是何物?直教AI沉默、Web寡言(1)
javascript·c#·.net·web
稀饭过霍24 分钟前
【.NET 10.0】使用FluentValidation
c#·mvc·.net
悟能不能悟33 分钟前
java list=null,可以stream吗
java·windows·list
love530love41 分钟前
【笔记】华硕 ROG MAXIMUS Z890 HERO 主板 BIOS 更新完整操作实录
运维·人工智能·windows·笔记·单片机·嵌入式硬件·bios
HUST43 分钟前
C 语言 第七讲:数组和函数实践:扫雷游戏
c语言·开发语言·数据结构·vscode·算法·游戏·c#
magic_kid_20101 小时前
如何排查和解决 Windows 机器的莫名关机问题
windows·异常关机
坚果派·白晓明1 小时前
Windows 11 OpenHarmony 版 Flutter 开发环境搭建完整指南
windows·flutter·开源鸿蒙·鸿蒙跨平台应用
youngee111 小时前
hot100-49前缀树
开发语言·c#
Dillon Dong1 小时前
从C到SIMULINK: 字节/字偏移 + 位偏移实现故障与故障字保存操作
c语言·开发语言·c#
m5655bj2 小时前
如何通过 C# 将 Markdown 转换为 PDF 文档
开发语言·pdf·c#