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>
相关推荐
聪慧的水蜜桃1 小时前
【YFIOs】用C#开发硬件之设备上云
开发语言·c#
caimouse4 小时前
Windows Research Kernel (WRK) 内存管理器完整分析
windows
济*沧*海4 小时前
BaseMapper的介绍
java·windows·tomcat
vortex54 小时前
PowerShell Provider 机制详解:从文件系统到注册表的统一抽象
windows·powershell
hixiong1236 小时前
TensorRT转换工具分享
人工智能·计算机视觉·ai·c#
小狮子&7 小时前
ubuntu2604终端右键新建窗口
windows
莫生灬灬8 小时前
DY联系人聊天Web面板支持获取联系人/聊天记录/发送消息
前端·chrome·websocket·c#
shx_wei8 小时前
从函数调用到进程替换:使用 execl、CMake 与 PowerShell 构建一个多可执行程序
linux·c语言·windows·c
奶油话梅糖8 小时前
锐捷常用命令速查表
java·网络·windows
love530love9 小时前
CodexPro + MCP + Cloudflare 配置详解:HTTP2、QUIC 与连接排障
ide·人工智能·windows·ai agent