在console程序中,如何捕捉console程序的关闭信息呢? I think you can not 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 能做到,只能用API来实现,不过麻烦点而已:下面代码刚刚做的,测试通过:======================================================using System;using System.Windows.Forms;using System.Diagnostics;using System.Runtime.InteropServices;namespace WindowsApplication2{public delegate bool ConsoleCtrlDelegate(int dwCtrlType);public class ClsMain { //The SetConsoleCtrlHandler function adds or removes an application-defined HandlerRoutine function //from the list of handler functions for the calling process. [DllImport("kernel32.dll")] private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine,bool Add); //一個Ctrl + C的信號被接收,該信號或來自鍵盤,或來自GenerateConsoleCtrlEvent 函數 private const int CTRL_C_EVENT = 0; //一個 Ctrl + Break 信號被接收,該信號或來自鍵盤,或來自GenerateConsoleCtrlEvent 函數 private const int CTRL_BREAK_EVENT = 1; //當用戶系統關閉Console時,系統會發送此信號到此 private const int CTRL_CLOSE_EVENT = 2; //當用戶退出系統時系統會發送這個信號給所有的Console程序。該信號不能顯示是哪個用戶退出。 private const int CTRL_LOGOFF_EVENT = 5; //當系統將要關閉時會發送此信號到所有Console程序 private const int CTRL_SHUTDOWN_EVENT = 6; [STAThread] static void Main() { ClsMain cls=new ClsMain(); } public ClsMain() { // 用API安装事件处理 ConsoleCtrlDelegate newDelegate=new ConsoleCtrlDelegate(HandlerRoutine); bool bRet=SetConsoleCtrlHandler(newDelegate,true); if(bRet==false) //安装事件处理失败 { Debug.WriteLine("失败"); } else { Console.WriteLine("ok"); Console.Read(); } } /// <summary> /// 处理消息的事件 /// </summary> private static bool HandlerRoutine(int CtrlType) { switch(CtrlType) { case CTRL_CLOSE_EVENT: //用户要关闭Console了 Debug.WriteLine("Close"); break; } return false; }}} 晕,贴出来格式全变了,CSDN的广告站了大半的位置了,楼主看的时候注意一下,很多注释前面没有显示//,下面的代码是一样的,不过把注释全删了,看注释就看上面的:-----------------------------------------------------------public delegate bool ConsoleCtrlDelegate(int dwCtrlType);public class ClsMain { [DllImport("kernel32.dll")]private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine,bool Add); private const int CTRL_C_EVENT = 0; private const int CTRL_BREAK_EVENT = 1; private const int CTRL_CLOSE_EVENT = 2; private const int CTRL_LOGOFF_EVENT = 5; private const int CTRL_SHUTDOWN_EVENT = 6; [STAThread] static void Main() { ClsMain cls=new ClsMain(); } public ClsMain() { ConsoleCtrlDelegate newDelegate=new ConsoleCtrlDelegate(HandlerRoutine); bool bRet=SetConsoleCtrlHandler(newDelegate,true); if(bRet==false) { Debug.WriteLine("失败"); } else { Console.WriteLine("ok"); Console.Read(); } } private static bool HandlerRoutine(int CtrlType) { switch(CtrlType) { case CTRL_CLOSE_EVENT: Debug.WriteLine("Close"); break; } return false; }} 上面的代码我是看到这个贴才写的,测试肯定通过了。如果楼主还没实验成功的话,就留下mail ,我发完全代码给你!!! 如何实现图像旋转 获取地址栏的地址 girdview的问题(高手请帮帮忙,急啊!!!) 在ACCESS里怎么设置数据库密码啊??? C#在程序中怎么运行C#代码来执行C#程序 如何判断网页报没报错 c#怎样对ftp进行操作啊 各位大哥有没有漂亮的字体控件,谢谢 如何在.net环境下控制多媒体? 郁闷啊,乱码问题......请会的进来看看 请问如何序列化一个自定义类,同时能通过web service调用。 如何在vs.net2003的WEB页面里用用户控件建立一个下拉采单?在线等,马上结分!
======================================================
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;namespace WindowsApplication2
{public delegate bool ConsoleCtrlDelegate(int dwCtrlType);public class ClsMain
{
//The SetConsoleCtrlHandler function adds or removes an application-defined HandlerRoutine function
//from the list of handler functions for the calling process.
[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine,bool Add);
//一個Ctrl + C的信號被接收,該信號或來自鍵盤,或來自GenerateConsoleCtrlEvent 函數
private const int CTRL_C_EVENT = 0;
//一個 Ctrl + Break 信號被接收,該信號或來自鍵盤,或來自GenerateConsoleCtrlEvent 函數
private const int CTRL_BREAK_EVENT = 1;
//當用戶系統關閉Console時,系統會發送此信號到此
private const int CTRL_CLOSE_EVENT = 2;
//當用戶退出系統時系統會發送這個信號給所有的Console程序。該信號不能顯示是哪個用戶退出。
private const int CTRL_LOGOFF_EVENT = 5;
//當系統將要關閉時會發送此信號到所有Console程序
private const int CTRL_SHUTDOWN_EVENT = 6; [STAThread]
static void Main()
{
ClsMain cls=new ClsMain();
}
public ClsMain()
{
// 用API安装事件处理
ConsoleCtrlDelegate newDelegate=new ConsoleCtrlDelegate(HandlerRoutine);
bool bRet=SetConsoleCtrlHandler(newDelegate,true);
if(bRet==false) //安装事件处理失败
{
Debug.WriteLine("失败");
}
else
{
Console.WriteLine("ok");
Console.Read();
}
}
/// <summary>
/// 处理消息的事件
/// </summary>
private static bool HandlerRoutine(int CtrlType)
{
switch(CtrlType)
{
case CTRL_CLOSE_EVENT: //用户要关闭Console了
Debug.WriteLine("Close");
break;
} return false;
}
}
}
-----------------------------------------------------------
public delegate bool ConsoleCtrlDelegate(int dwCtrlType);public class ClsMain
{
[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine,bool Add);
private const int CTRL_C_EVENT = 0;
private const int CTRL_BREAK_EVENT = 1;
private const int CTRL_CLOSE_EVENT = 2;
private const int CTRL_LOGOFF_EVENT = 5;
private const int CTRL_SHUTDOWN_EVENT = 6; [STAThread]
static void Main()
{
ClsMain cls=new ClsMain();
}
public ClsMain()
{
ConsoleCtrlDelegate newDelegate=new ConsoleCtrlDelegate(HandlerRoutine);
bool bRet=SetConsoleCtrlHandler(newDelegate,true);
if(bRet==false)
{
Debug.WriteLine("失败");
}
else
{
Console.WriteLine("ok");
Console.Read();
}
}
private static bool HandlerRoutine(int CtrlType)
{
switch(CtrlType)
{
case CTRL_CLOSE_EVENT:
Debug.WriteLine("Close");
break;
} return false;
}
}