Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从系统中获取注销事件_C#_.net_Wpf_Logout - Fatal编程技术网

C# 从系统中获取注销事件

C# 从系统中获取注销事件,c#,.net,wpf,logout,C#,.net,Wpf,Logout,我正在做一个应用程序,它是用来清除临时文件,历史等,当用户注销。因此,我如何知道系统是否要注销(在C中)?您可以使用WMI并观看Win32_ComputerShutdownEvent,其中Type等于0。您可以找到有关此事件的详细信息,以及有关在.NET中使用WMI的详细信息。在环境中有一个属性告诉您关闭过程是否已启动: Environment.HasShutDownStarted 但在谷歌搜索之后,我发现这可能对你有所帮助: using Microsoft.Win32; //durin

我正在做一个应用程序,它是用来清除临时文件,历史等,当用户注销。因此,我如何知道系统是否要注销(在C中)?

您可以使用WMI并观看Win32_ComputerShutdownEvent,其中Type等于0。您可以找到有关此事件的详细信息,以及有关在.NET中使用WMI的详细信息。

环境中有一个属性告诉您关闭过程是否已启动:

Environment.HasShutDownStarted
但在谷歌搜索之后,我发现这可能对你有所帮助:

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

但是,如果您只想清除临时文件,那么我认为区分关闭或注销对您没有任何影响。

如果您特别需要注销事件,您可以修改VillageDiot回答中提供的代码,如下所示:

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

如果您有一个Windows窗体,您可以处理
FormClosing
事件,然后检查
e.CloseReason
枚举值以确定它是否等于
CloseReason.WindowsShutDown

private void FormMain\u FormClosing(对象发送方,FormClosingEventArgs e)
{
如果(e.CloseReason==CloseReason.WindowsShutDown)
{
//你的代码在这里
}
}

但请记住,在Vista+上,您在关机期间几乎没有时间做任何事情,因此请确保您不能出于任何原因(例如,试图删除网络共享上的文件等)而阻止或等待谢谢@Paul那么建议在某个地方输入一个条目,以便windows在下次重新启动或登录时清除这些内容吗?什么是SystemEvents?关机、注销…使用LAN唤醒关机?