How to get C# to detect first windows logon since computer startup?

78 Views Asked by At

I'm a beginner in C# programming and have absolutely no idea how to solve the following puzzle (no matter the program language).

As little explanation, I am about to write a program restoring custom startup sound for Windows Vista (and higher), running non-elevated, without being a service and placed in current users Autostart. But my app should play the startup sound ONLY, if MS doesn't already play the logon sound. This should be determined by the following dummy script:

if (CurrentUserHasAutoLogin = Yes OR (CurrentUserAccountIsActiveOnly = Yes AND NoPasswordSet AND NotInDomain))
AND **SinceLastRestartItstheVeryFirstLogin = Yes**
{
Play.Startup.Sound
} else {
//do nothing (Windows plays Logon sound instead)
}

Its almost done yet, except the bold typed part. It is the condition "SinceLastRestartItstheVeryFirstLogin" I have problems with. Here is what I have tried already (and expect to work):

  • The code should at least be compatible down to Windows Vista/7 (my current target framework is 4.0)
  • Since its an autostart app and have to be installed by user rights, it won't survive a re-logon.
  • My first attempt was to query WTSGetActiveConsoleSessionId, but unfortunately on Windows Vista/7 the counter will be resetted to 0 once you logout/login with same user again (on Windows 10/11 it works, but my app should work for Vista/7 too), so every logon 1 would be displayed.
  • The idea of creating a registry/file counter entry after first logon and delete when shutdown isn't perfect too since abnormal shutdown (e.g. power outage) might interfere with it. It should be determined without redirected input.
  • A (dirty) workaround might be searching for SYSTEM process SearchIndexer.exe since it gets started after the first logon and will outlast logout. But some people tend to disable Windows Search for better performance and the process never starts then, so its not reliable. Anyway, this method seems kind of weird.

Does anyone have an idea how to identify whether it's the first Windows logon since computer started or not, when app runs with non-elevated rights and invoked by Autostart?

0

There are 0 best solutions below