Get Computer State (Locked, Stand By) in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suryahg
    New Member
    • Feb 2008
    • 2

    Get Computer State (Locked, Stand By) in C#

    Hi,

    I would like to get the status of System State like whether its Locked, or Logged.
    I am using C# and application is WinForm.

    Advance thanks for the help

    Regards,
    Surya
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I think if you found out the currently logged in user, you would get your result.
    If there is no logged in user, then you're logged out.
    I suppose if someone LOCKS the pc, then they are still logged in. Hmm. Maybe cycling through the running proccess (System.Diagnos tics.Process.Ge tCurrentProcess ()) and looking at the user names of running programs could be used as well.


    For currently logged on user, I would take a look at:
    Code:
    System.Security.Principal.WindowsIdentity.GetCurrent()
    and it's overloads to see what is correct usage.



    There may in fact be a much easier solution, but I can't think of it right now.

    I also found this:

    Comment

    • suryahg
      New Member
      • Feb 2008
      • 2

      #3
      Hi,
      I found the solution.
      Code:
      using Microsoft.Win32;
      
      Constrctor()
      {
      SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
      }
      
      ~Constrctor()
      {
      //Do this during application close to avoid handle leak
      Microsoft.Win32.SystemEvents.SessionSwitch -= new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
      }
      
      
      void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
              {
                  switch (e.Reason)
                  {
                      case SessionSwitchReason.SessionLogon:
                      case SessionSwitchReason.SessionUnlock:
                          break;
      
                      case SessionSwitchReason.SessionLock:
                      case SessionSwitchReason.SessionLogoff:
      
                          break;
                  }
              }
      Event is fired when ever Pc is locked, Unlocked....

      Thank You
      Surya

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Excellent find .

        Comment

        Working...