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>
相关推荐
yaoxin5211232 小时前
324. Java Stream API - 实现 Collector 接口:自定义你的流式收集器
java·windows·python
非凡ghost3 小时前
小X分身APP(手机分身类工具)
android·windows·学习·智能手机·软件需求
两千次3 小时前
图像的处理 图片裁剪工具方法 图片按比例缩放的工具方法
c#
Sunsets_Red3 小时前
浅谈随机化与模拟退火
java·c语言·c++·python·算法·c#·信息学竞赛
Bruce_Liuxiaowei4 小时前
渗透测试中的提权漏洞:从低权限到系统控制的全解析
网络·windows·安全
kaizq4 小时前
Windows下基于Python构造Dify可视应用环境[非Dock]
windows·python·dify·大语言模型llm·人工智能ai·智能体agent
取个名字太难了a5 小时前
Win7 x64 内存管理(一)
windows
两千次6 小时前
图像的处理 图像转haclon
c#
程序员陆通8 小时前
零基础零成本把Clawdbot(OpenClaw)在Windows电脑上部署起来
windows
盘古工具9 小时前
告别重复数据:Excel禁止重复输入的两种方法
windows·excel