怎样捕捉控制台退出事件 试用了 AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnCurrentDomain_ProcessExit);不成功。请问有什么方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 退出时候弹出 提示框CLOSE(之前要添加using System.Windows.Forms 这个引用 看到效果) using System;using System.Diagnostics;using System.Runtime.InteropServices;using System.Windows.Forms;namespace ConsoleApplication2{ public delegate bool ConsoleCtrlDelegate(int ctrlType); class Program { [DllImport("kernel32.dll")] private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add); //当用户关闭Console时,系统会发送次消息 private const int CTRL_CLOSE_EVENT = 2; //Ctrl+C,系统会发送次消息 private const int CTRL_C_EVENT = 0; //Ctrl+break,系统会发送次消息 private const int CTRL_BREAK_EVENT = 1; //用户退出(注销),系统会发送次消息 private const int CTRL_LOGOFF_EVENT = 5; //系统关闭,系统会发送次消息 private const int CTRL_SHUTDOWN_EVENT = 6; static void Main(string[] args) { Program cls = new Program(); //Console.ReadKey(); } public Program() { ConsoleCtrlDelegate consoleDelegete = new ConsoleCtrlDelegate(HandlerRoutine); bool bRet = SetConsoleCtrlHandler(consoleDelegete, true); if (bRet == false) //安装事件处理失败 { Debug.WriteLine("error"); } else { Console.WriteLine("ok"); Console.Read(); } } private static bool HandlerRoutine(int ctrlType) { switch (ctrlType) { case CTRL_C_EVENT: MessageBox.Show("C"); break; case CTRL_BREAK_EVENT: MessageBox.Show("BREAK"); break; case CTRL_CLOSE_EVENT: MessageBox.Show("CLOSE"); break; case CTRL_LOGOFF_EVENT: break; case CTRL_SHUTDOWN_EVENT: break; } //return true;//表示阻止响应系统对该程序的操作 return false;//忽略处理,让系统进行默认操作 } }} WPF 中的ListView,在加载数据时不是全部加载,在哪设置以禁用此功能? 请教如何读取文本文件进行筛选后导入到datatable中 word文档的唯一标示 求个正则匹配两个连续的HTML标签并取出标签中的值 关于64位win7上用VS开发的问题 请问在VS2005中的C#中如何连接数据啊 送分100分:关于数组和变量互相赋值的问题 关于动态创建类实例的问题 大家好,本人急求 没分的看过来!给前5个三角少的:) Wpf 或 Silverlight 中 RichTextBox 实现文本竖排 C#中的数组能像C中那样偏移吗?
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace ConsoleApplication2
{ public delegate bool ConsoleCtrlDelegate(int ctrlType);
class Program
{
[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add);
//当用户关闭Console时,系统会发送次消息
private const int CTRL_CLOSE_EVENT = 2;
//Ctrl+C,系统会发送次消息
private const int CTRL_C_EVENT = 0;
//Ctrl+break,系统会发送次消息
private const int CTRL_BREAK_EVENT = 1;
//用户退出(注销),系统会发送次消息
private const int CTRL_LOGOFF_EVENT = 5;
//系统关闭,系统会发送次消息
private const int CTRL_SHUTDOWN_EVENT = 6; static void Main(string[] args)
{
Program cls = new Program();
//Console.ReadKey();
}
public Program()
{
ConsoleCtrlDelegate consoleDelegete = new ConsoleCtrlDelegate(HandlerRoutine); bool bRet = SetConsoleCtrlHandler(consoleDelegete, true);
if (bRet == false) //安装事件处理失败
{
Debug.WriteLine("error");
}
else
{
Console.WriteLine("ok");
Console.Read();
} } private static bool HandlerRoutine(int ctrlType)
{
switch (ctrlType)
{
case CTRL_C_EVENT:
MessageBox.Show("C");
break;
case CTRL_BREAK_EVENT:
MessageBox.Show("BREAK");
break;
case CTRL_CLOSE_EVENT:
MessageBox.Show("CLOSE");
break;
case CTRL_LOGOFF_EVENT:
break;
case CTRL_SHUTDOWN_EVENT:
break;
}
//return true;//表示阻止响应系统对该程序的操作
return false;//忽略处理,让系统进行默认操作
}
}}