Check if no user is currently logged on to Windows
Another option, if you don't want to deal with the P/Invokes: use Cassia.
using Cassia;
public static bool IsSomeoneLoggedOn(string server)
{
foreach (ITerminalServicesSession session in new TerminalServicesManager().GetSessions(server))
{
if (!string.IsNullOrEmpty(session.UserName))
{
return true;
}
}
return false;
}
Use WTSGetActiveConsoleSessionId()
to determine whether anybody is logged on locally. Use WTSEnumerateSessions()
to determine if there is any session at all (including remote terminal services sessions).