Logged on desktop user?

G

GTi

Is there any way I can find out if there is any user logged on the
desktop from a service?
 
N

Nicholas Paldino [.NET/C# MVP]

GTi,

Yes, but you shouldn't do this. First, there might not be a logged in
user, and second, there may be multiple logged in users.

What you want to do is have your service expose a remoting endpoint, and
then have a program that runs in the client space which connects to the
service and passes/gets the appropriate information.

Hope this helps.
 
G

GTi

Nicholas said:
GTi,

Yes, but you shouldn't do this. First, there might not be a logged in
user, and second, there may be multiple logged in users.

What you want to do is have your service expose a remoting endpoint, and
then have a program that runs in the client space which connects to the
service and passes/gets the appropriate information.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

GTi said:
Is there any way I can find out if there is any user logged on the
desktop from a service?

Nicholas,
Yeah - after walking round the blocks a while I ended with the same
conclusion.
Since my NT Service application is almost always depended on other
programs that only can run on the users desktop, I will change my
program to do the same as a icon by the clock.
(Note to me self: how do I do that?)
 
J

Jon Skeet [C# MVP]

GTi said:
Yeah - after walking round the blocks a while I ended with the same
conclusion.
Since my NT Service application is almost always depended on other
programs that only can run on the users desktop, I will change my
program to do the same as a icon by the clock.
(Note to me self: how do I do that?)

If you do a google search for "system tray C#" you'll find lots of
hits, including
http://www.developer.com/net/csharp/article.php/3336751

Jon
 
W

Willy Denoyette [MVP]

GTi said:
Is there any way I can find out if there is any user logged on the
desktop from a service?

Yep, using System.Management classes.

...

ManagementScope msc = new ManagementScope("root\\cimv2");
// get the number of interactive logons (logontype = 2)
string queryString = "select LogonId from win32_logonsession where
logontype = 2";
using(ManagementObjectSearcher query = new ManagementObjectSearcher(msc,
new SelectQuery(queryString)))
{
if (query.Get().Count == 0) // 0 = no interactive user logon
...
else
...
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top