请从系统注销事件事件、系统

2023-09-03 03:58:37 作者:偶尔被需要,从来不重要

我做的这是用来清除临时文件,历史记录等,当用户注销的应用程序。所以,我怎么能知道,如果系统要注销(在C#)?

I am doing an application which is used to clear the Temp files, history etc, when the user log off. So how can I know if the system is going to logoff (in C#)?

推荐答案

有一个在环保类属性的讲述,如果关机过程已经启动:

There is a property in Environment class that tells about if shutdown process has started:

Environment.HasShutDownStarted

但一些谷歌上搜索后,我发现,这可能是帮助你:

But after some googling I found out that this may be of help to you:

 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
     }
  }

但是,如果你只希望清除临时文件,然后我想关机区分或注销的任何后果不给你。

But if you only want to clear temp file then I think distinguishing between shutdown or log off is not of any consequence to you.