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>
相关推荐
Never_Satisfied1 小时前
C#获取汉字拼音字母方法总结
开发语言·c#
私人珍藏库4 小时前
[Windows] EndNote 2025.1.19456 汉化版
windows·工具·软件·win·汉化
love530love4 小时前
告别环境崩溃:ONNX 与 Protobuf 版本兼容性指南
人工智能·windows·python·onnx·stablediffusion·comfyui·protobuf
weixin_516023074 小时前
SRIM的安装
windows
不绝1917 小时前
C#核心:多态
开发语言·c#
我的大老婆7 小时前
【Windows 共享打印机纯干货】跨路由器/跨网段访问打印机、两条宽带两个路由器共享一台打印机、外网/局域网外访问DMZ打印机、192.168.1.107 主机访问192.168.2.105 打印机
windows·经验分享·计算机网络·路由器·共享打印机·跨网段共享打印机·dmz主机
橙露8 小时前
C#在视觉检测中的优势:工业智能化转型的利器
开发语言·c#·视觉检测
xiaoliuliu123459 小时前
ChromeStandalone_58.0.3029.110使用步骤详解(附安装与设置教程)
windows
爱说实话9 小时前
C# DependencyObject类、Visual类、UIElement类
开发语言·c#
TDengine (老段)9 小时前
TDengine C# 语言连接器进阶指南
大数据·数据库·人工智能·物联网·c#·时序数据库·tdengine