Khalid Rahaman
Khalid Rahaman

Reputation: 2302

Detecting windows shutdown event

I have a windows Service always running once PC started, how would i detect using .NET 3.5 or WMI, when the user or some other application/process has requested a shutdown or reboot in windows.

I do not want to stop it, just log the time and who initiated the shutdown (user or process is fine, don't really need to know which process)

Thank you

Upvotes: 4

Views: 6747

Answers (3)

Michael Petrotta
Michael Petrotta

Reputation: 60972

You're looking for the SystemEvents.SessionEnding event.

Occurs when the user is trying to log off or shut down the system.

Upvotes: 1

codekaizen
codekaizen

Reputation: 27429

Use the Microsoft.Win32.SystemEvents.SessionEnding event.

However, note that you are in a Windows Service, and this event won't fire for you unless you create a hidden form in your service. You can get one of these for free by enabling the "Interact with Desktop" option on the service.

Upvotes: 4

cmptrer
cmptrer

Reputation: 1449

Win32.SessionEnded event gets fired when the system is being shut down. You should attach to this in your program.

Upvotes: 0

Related Questions