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>
相关推荐
雨浓YN3 小时前
OPC UA 通讯开发笔记 - 基于本地dll文件
windows·笔记
大强同学4 小时前
Obsidian CLI + Claude Code = 王炸组合
人工智能·windows·ai编程·cli
iceslime5 小时前
Windows10系统静音修复相关
windows·音频·修复
humors2215 小时前
AI工具合集,不定期更新
人工智能·windows·ai·工具·powershell·deepseek
Echo-J5 小时前
在 Windows 7 虚拟机上安装 VMware Tools 时遇到驱动无法安装的问题
windows
山檐雾10 小时前
OctreeNode
unity·c#·八叉树
sR916Mecz11 小时前
pache Hop实战:Windows平台MySQL数据迁移的深度排错与性能调优
数据库·windows·mysql
小白电脑技术12 小时前
Windows家庭版开启原生远程桌面
windows
扑火的小飞蛾13 小时前
Windows 10 与 Kali Linux SSH 免密互信配置指南
linux·windows·ssh
QfC92C02p13 小时前
C# 中的 Span 和内存:.NET 中的高性能内存处理
java·c#·.net