我爱Aspx >> C#.Net >> 使用c#捕获windows的关机事件_C#应用
在公司上班,下班时需要签退,而我呢隔三差五就会忘那么一次。怎么办呢,于是就想能不能捕获windows的关机事件,做一个程序让它在关机的时候提醒我一下呢。
非常幸运很容易就找到了Microsoft.Win32命名空间下面的SystemEvents类,他有一个静态的事件SessionEnding在系统注销或者关机时发生,此事件只有在winform的程序下有效,而在控制台程序下面无效,不能激发事件;还有一点我们必须在程序推出时将加上的事件移除掉,否则就容易造成内存溢出。
关键代码如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
static class Program
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormShutdown formShutdown = new FormShutdown();
SystemEvents.SessionEnding += new SessionEndingEventHandler(formShutdown.SystemEvents_SessionEnding);
Application.Run(formShutdown);
}
}
}Form 的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
public partial class FormShutdown : Form
{
const string MESSAGE_TXT = "您签退了吗?";
const string MESSAGE_TITLE = "提示";
public FormShutdown()
{
InitializeComponent();
}
internal void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
Ҷƪл˵?
关于正则表达式匹配无异常资源耗..[04-28]
C#分析数据库结构,使用XSL模板自..[04-28]
在c#中执行sql语句时传递参数的小..[04-28]
c#时间增加并对比做法_C#教程[04-28]
C# 编码规范和编程好习惯_C#教程[04-28]
c#2.0泛型学习(一) _C#教程[04-28]
c#泛型学习(二)_C#教程[04-28]
C# 3.0新特性初步研究 Part1:使用..[04-28]
C# 3.0新特性初步研究 Part2:使用..[04-28]
C# 3.0新特性初步研究 Part3:使用..[04-28]
关于正则表达式匹配无异常资源耗..[04-28]
C#分析数据库结构,使用XSL模板自..[04-28]
在c#中执行sql语句时传递参数的小..[04-28]
c#时间增加并对比做法_C#教程[04-28]
C# 编码规范和编程好习惯_C#教程[04-28]
c#2.0泛型学习(一) _C#教程[04-28]
c#泛型学习(二)_C#教程[04-28]
C# 3.0新特性初步研究 Part1:使用..[04-28]
C# 3.0新特性初步研究 Part2:使用..[04-28]
C# 3.0新特性初步研究 Part3:使用..[04-28]