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>
相关推荐
飞Link1 小时前
Windows 环境下 LaTeX 的安装配置教程
windows·vscode
R.lin2 小时前
windows MySQL解压版安装教程
windows·mysql·adb
常家壮2 小时前
Windows隐藏账号创建完全指南:技术原理与安全实践
windows·安全·渗透测试·后门·windows隐藏账号
c#上位机4 小时前
halcon图像增强——emphasize
图像处理·人工智能·计算机视觉·c#·上位机·halcon
真上帝的左手5 小时前
18. 操作系统-Windows-命令提示符
windows
zxy28472253015 小时前
C#的视觉库Halcon入门示例
c#·图像识别·halcon·机器视觉
GHL2842710907 小时前
文件重命名(C++源码)
前端·c++·windows
fred_kang8 小时前
win11上使用SecoClient登录报错出现的SV无法连接至服务器信息
windows
丁劲犇10 小时前
Visual C++下使用Win32 API为Release模式导出崩溃堆栈
c++·windows·crash·dump·离线调试·vc·崩溃堆栈
半夏知半秋10 小时前
kibana介绍与部署(Windows详细版)
大数据·运维·windows·学习·搜索引擎·全文检索