Liviu Mandras
Liviu Mandras

Reputation: 6617

Detect when user logs back in

I have a winforms app (C# .net 2.0) that is running in sys tray. The user can log off from the system and is presented with Windows' usual login screen.

I want to detect when user logs back in.

I know there is a SessionEnding event to detect when user logs out. I need the reverse of that event if exists.

I need to take special action only when the app is started as a result of "log off-log back in" sequence and not when computer (OS actually) boots up.

Upvotes: 1

Views: 637

Answers (1)

Cody Gray
Cody Gray

Reputation: 244772

When a user logs off, all of their running applications are closed, including your WinForms app that is minimized to the taskbar's notification area. The purpose of the SessionEnding event is for you to shut down your app gracefully when the user logs off or the computer is shutting down. There is no corresponding SessionBeginning event because your app is not automatically reopened when the user logs back in.

If you want your application to be automatically started whenever the user logs in, you need to place it in the "Startup" folder. All of the programs in that folder will be started automatically when the user logs in.

You can either configure your installer to place the application in the user's "Startup" folder (use the CSIDL_STARTUP environment variable to find the correct location) or the common "Startup" folder shared by all users (use the CSIDL_COMMON_STARTUP environment variable). Of course, if you don't have an installer, you can always place a shortcut to your application into that folder manually.

Upvotes: 4

Related Questions