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>
相关推荐
REN者无敌3 小时前
桌面图标变白?Win10/Win11 通用修复方法:5 步解决,附原理说明
windows
独隅5 小时前
Ollama Windows 安装与使用全指南:零配置本地运行 Llama、DeepSeek 等大模型,保障隐私与高效体验
windows
keep the6 小时前
Windows启动IOS17/18的WDA
windows·ios·自动化
取个名字太难了a6 小时前
intel白皮书卷2 第二章(AI翻译)
windows
爱编码的傅同学7 小时前
【线程同步】信号量与环形队列的生产消费模型
linux·windows·ubuntu·centos
专注VB编程开发20年8 小时前
如何强制ANY CPU的.net程序按32位或64位模式运行?
windows·.net
yngsqq10 小时前
批量裁剪——CAD一键根据裁剪框裁剪
c#
yaoxin52112310 小时前
303. Java Stream API - 查找元素
java·windows·python
z.q.xiao12 小时前
【镜像模式】WSL如何访问windows内网服务
linux·网络·windows·gitlab·wsl·dns
gf132111112 小时前
python_生成RPA运行数据报告
windows·python·rpa