Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
3.70/5 (3 votes)
See more:
Hi
I am developing a Windows service in C# that has to monitor remote desktop logins that are made to the machine that it is running on and send the status to a server.
I was using the following piece of code to determine if it is a Remote Desktop Session or not
C#
if (System.Windows.Forms.SystemInformation.TerminalServerSession == true)
       {
           isRemoteSession = true;
       }
       else
       {
           isRemoteSession = false;
       }


What I realized is that this particular property "TerminalServerSession" is NOT set to false as soon as I CLOSE my remote desktop window. It is being set to false only when I do a normal login to that particular machine.

My requirement is to detect the status as soon as the Remote Desktop has been closed.
Can anyone please help me out here?

Thanks in advance!
Posted

have you read everything from the buffer? If there is information in the buffer to be read, then it might not pick up the disconnected state until the buffer is cleared.

Hope this will give you an idea.
 
Share this answer
 
Comments
lydiae 2-Mar-11 2:16am    
I am not sure which buffer you are talking about.
Can you please explain?
Sergey Alexandrovich Kryukov 2-Mar-11 3:23am    
How can you imagine that? Buffer can be empty simply due to inactivity even is a working session, don't you think so?
--SA
Hi,

you should listen to:
C#
SystemEvents.SessionSwitch += new SessionSwitchEventHandle SystemEvents_SessionSwitch);


In the Event Args there is a SessionSwitchReason(e.Reason) : SessionLock, SessionUnlock etc. and there is already
RemoteDisconnect and RemoteConnect
C#
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
    if (e.Reason == SessionSwitchReason.SessionLock || e.Reason == SessionSwitchReason.RemoteDisconnect || e.Reason == SessionSwitchReason.SessionLogoff)
    {

    }
    else if (e.Reason == SessionSwitchReason.SessionUnlock || e.Reason == SessionSwitchReason.RemoteConnect || e.Reason == SessionSwitchReason.SessionLogon)
    {

    }
}


Hope that helps
 
Share this answer
 
Comments
lydiae 2-Mar-11 2:33am    
I think this may work. I am pretty new to C#. If you know any links that have examples for this can you please post it?
Thanks a lot!
DrNimnull 2-Mar-11 3:03am    
Try Google =)
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionswitch(v=vs.80).aspx

What samples do you want? Look up isn´t it an example?
lydiae 2-Mar-11 3:35am    
Your example seems to have errors and also it is not a complete program. Since I am new to C# I was looking for complete examples.
Thanks for the link!
DrNimnull 4-Mar-11 7:10am    
If you say: 'Your example seems to have errors'...the question is what are the errors? Of course
my post is no complete program! What are your troubles with this approach?
Not an Answer, not related to the Question.

Writing (see the Question)
C#
if (System.Windows.Forms.SystemInformation.TerminalServerSession == true)
       {
           isRemoteSession = true;
       }
       else
       {
           isRemoteSession = false;
       }

instead of
C#
isRemoteSession = System.Windows.Forms.SystemInformation.TerminalServerSession;

leaves me without much hope.

Besides, System.Windows.Forms.SystemInformation.TerminalServerSession property indicate that the application (calling process) is run on a server computer as a part of remote session. It is always either true of false, cannot detect connection or disconnection. And certainly it will be false for your Windows Service. This check cannot help you at all.

Sorry, Lydiae. Just think about it.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900