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#)?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
If you have a Windows Form, you can handle the
FormClosing
event, then check thee.CloseReason
enum value to determine if it equals toCloseReason.WindowsShutDown
.There is a property in Environment class that tells about if shutdown process has started:
But after some googling I found out that this may be of help to you:
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.
You can use WMI and watch the Win32_ComputerShutdownEvent where Type is equal to 0. You can find more information about this event here, and more about using WMI in .NET here.
If you specifically need the log-off event, you can modify the code provided in TheVillageIdiot's answer as follows: