惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 血狼

切换ThinkPad Fn键 window.event.keycode值大全 通用分页存储过程 Oracle中将密码有效期由默认的180天修改成“无限制” C#中动态编译某C#文件 C#程序中执行script .Net Reactor 加密的简单例子 MSBuild的相关操作 程序中执行cmd.exe 北京嘉纳博科技有限公司 Windows Service 初始化代码中追加断点问题 C#定时执行某个程序 .Net中关于多个FrameWork的问题 Chilkat 收、送信 合并文件 设置和获取注册表数据 DotNet多个程序集合并工具 PostGres 全文检索 PostGres 中包含触发器的方法
使用c#捕获Windows的关机事件
血狼 · 2008-07-30 · via 博客园 - 血狼

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)
        
{
            DialogResult result 
= MessageBox.Show(MESSAGE_TXT, MESSAGE_TITLE, MessageBoxButtons.YesNo);

            e.Cancel 
= (result == DialogResult.No);
        }


        
private void FormShutdown_Load(object sender, EventArgs e)
        
{
            
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - 2000);
        }


        
protected override void OnClosed(EventArgs e)
        
{
            SystemEvents.SessionEnding 
-= new SessionEndingEventHandler(this.SystemEvents_SessionEnding);
            
base.OnClosed(e);
        }

    }

}

注意:

1、控制台应用程序不会引发 SessionEnding 事件。

2、因为这是一个静态事件,所以释放应用程序时必须分离事件处理程序,否则会导致内存泄漏。

参照

http://www.cnblogs.com/yukaizhao/archive/2007/04/23/csharp_catch_shutdown_events.html 

http://msdn.microsoft.com/zh-cn/library/microsoft.win32.systemevents.sessionending(VS.85).aspx