在C#中利用API编写关机程序

本文介绍了一个使用 C# 开发的简单应用程序,该程序能够根据设定的时间自动执行关机操作。通过调用 Windows API 函数 `ExitWindowsEx` 实现了定时关机的功能,并通过界面元素允许用户设置具体的日期和时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序界面如上

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Runtime.InteropServices;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace close_btnn
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         [DllImport("user32")]
  14.         public static extern long SetWindowPos(long hwnd, long hWndInsertAfter, long y, long cx, long cy, long wFkagslong);
  15.         [DllImport("user32")]
  16.         public static extern long ExitWindowsEx(long uFlags, long dwReserved);
  17.         [DllImport("user32")]
  18.         public static extern long ShellAbout(long uFlags, long dwReserved);
  19.         long dwReserved;
  20.         const int SHUTDOWN = 1;
  21.         const int REBOOT = 2;
  22.         const int zhx = 0;
  23.         long sh;
  24.         int counter, n;
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.       
  30.         private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
  31.         {
  32.         }
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             TxtTime.Text = System.DateTime.Today.ToShortDateString() + " " + System.DateTime.Today.ToLongTimeString() ;
  36.           
  37.         }
  38.         private void timer1_Tick(object sender, EventArgs e)
  39.         {
  40.             string CurrDate = System.DateTime.Today.ToShortDateString();
  41.             string CurrTime = System.DateTime.Today.ToShortTimeString();
  42.             if (this.checkBox1.Checked == true)
  43.             {
  44.                 if (CurrDate == setupdate.ToString() && CurrTime == setuptime.ToString())
  45.                     //添加调用close computer方法
  46.                     CloseComputer();
  47.             }
  48.            
  49.         }
  50.         private void CloseComputer()
  51.         {
  52.             sh = ExitWindowsEx(SHUTDOWN,dwReserved);
  53.         }
  54.         private void butreopen_Click(object sender, EventArgs e)
  55.         {
  56.             sh = ExitWindowsEx(SHUTDOWN, dwReserved);
  57.         }
  58.         private void butrelogin_Click(object sender, EventArgs e)
  59.         {
  60.             sh = ExitWindowsEx(zhx,dwReserved);
  61.         }
  62.         private void butcancel_Click(object sender, EventArgs e)
  63.         {
  64.             this.Close();
  65.         }
  66.         private void butclose_Click(object sender, EventArgs e)
  67.         {
  68.             sh = ExitWindowsEx(REBOOT, dwReserved);
  69.         }
  70.     }
  71. }

但运行会出错的。那位高手 帮忙看看。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值