Environment类中有一个属性,它告诉关闭进程是否已经启动:
Environment.HasShutDownStarted
Run Code Online (Sandbox Code Playgroud)
但经过一些谷歌搜索后,我发现这可能对你有所帮助:
using Microsoft.Win32;
//during init of your application bind to this event
SystemEvents.SessionEnding +=
new SessionEndingEventHandler(SystemEvents_SessionEnding);
void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (Environment.HasShutdownStarted)
{
//Tackle Shutdown
}
else
{
//Tackle log off
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您只想清除临时文件,那么我认为区分关闭或注销对您没有任何影响.
小智 8
如果您特别需要注销事件,则可以修改TheVillageIdiot答案中提供的代码,如下所示:
using Microsoft.Win32;
//during init of your application bind to this event
SystemEvents.SessionEnding +=
new SessionEndingEventHandler(SystemEvents_SessionEnding);
void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (e.Reason == SessionEndReasons.Logoff)
{
// insert your code here
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10218 次 |
| 最近记录: |