用C#怎么编写一个最简单的定时关机程序

来源:百度知道 编辑:UC知道 时间:2024/06/06 08:46:03
能不能在textBox输入时间,然后到了这个时间就关机,要怎么写?

//这里的[DllImport("user32.dll")]是.net里属性的表达方法,DllImport为引用系统API所在的库"user32.dll"

[DllImport("user32.dll")]
//主要API是这个,注意:必须声明为static extern
private static extern int ExitWindowsEx(int x,int y);

直接调用ExitWindowsEx(1,0); //主要调用系统API来实现
即可.

至于输入时间 你可以用个定时器判断当前的时间与输入时间的关系 如果>= 则立刻调用ExitWindowsEx(1,0); 就可以了!

textBox 的值获取就不用我说了吧?

string seconds = textBox.Value;
Process.Start("shutdown -s -t "+ seconds);

取消定时
Process.Start("shutdown -a");

(1)我是先添加了一个using System.IO;这个命名空间,为的是生成一个(,bat)文件,接着把textBox中的值用(at time shutdown -s)写进(.bat)接着用Process.Start("txt.bat");来执行它。Process的命名空间为using System.Diagnostics;
(2)下边是我写的程序你看看,我把程序写进了button-click事件中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.For