문제

사용자가 로그오프할 때 임시 파일, 기록 등을 지우는 데 사용되는 응용 프로그램을 만들고 있습니다.그러면 시스템이 로그오프될 것인지(C#에서) 어떻게 알 수 있습니까?

도움이 되었습니까?

해결책

속성이 있습니다 환경 종료 프로세스가 시작되었는지 알려주는 클래스 :

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

그러나 온도 파일 만 지우고 싶다면 셧다운이나 로그 오프를 구별하는 것은 어떤 결과도 안된다고 생각합니다.

다른 팁

로그 오프 이벤트가 필요한 경우 VillageIdiot의 답변에 제공된 코드를 다음과 같이 수정할 수 있습니다.

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

WMI를 사용하고 유형이 0 인 Win32_ComputersHutdownEvent를 볼 수 있습니다.이 이벤트에 대한 자세한 정보를 찾을 수 있습니다. 여기, .NET에서 WMI 사용에 대한 자세한 내용 여기.

당신이 가지고 있다면 윈도우 폼, 당신은 FormClosing 이벤트를 확인한 다음 e.CloseReason 다음과 같은지 확인하는 열거형 값 CloseReason.WindowsShutDown.

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.WindowsShutDown)
    {
        // Your code here
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top