質問
ユーザーがログオフしたときに、温度ファイル、履歴などをクリアするために使用されるアプリケーションを実行しています。では、システムが(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
}
}
しかし、あなたがTempファイルのみをクリアしたい場合、シャットダウンまたはログオフを区別することはあなたにとって何の結果ではないと思います。
他のヒント
ログオフイベントが特に必要な場合は、次のように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
}
}
あなたが持っている場合 Windowsフォーム, 、あなたは処理できます FormClosing
イベント、次にチェックします e.CloseReason
列挙値が等しいかどうかを判断します CloseReason.WindowsShutDown
.
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{
// Your code here
}
}
所属していません StackOverflow