Avatar of Arikkan
ArikkanFlag for United States of America

asked on 

Using VB.Net or C#.Net, - List active user logged into a machine and kill the session

We would like to build a simple program that lists users that are logged into the machine and by selecting the item on the list, we want to kill (log off) the session.

Could you please help in
1. code to get list of active session
2. code to log off the specific session (possibly by seeding ID from the #1 above)

(This is somewhat like what we can do from the Task Manager by selecting the user session and "Sign off")

User generated image
Visual Basic.NETC#

Avatar of undefined
Last Comment
Arikkan
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Avatar of Arikkan
Arikkan
Flag of United States of America image

ASKER

Thank you very much, but we wish to handle them within .Net.  Is there a way to do this without Powershell?  
Avatar of Arikkan
Arikkan
Flag of United States of America image

ASKER

Thank you but the first link is linking to a site that does not exist.
The second one may be ok with getting a list, but there is no way for terminating the session even we get a list...
ASKER CERTIFIED SOLUTION
Avatar of Andrei Fomitchev
Andrei Fomitchev
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Arikkan
Arikkan
Flag of United States of America image

ASKER

Hi Andrei,
This code currently disconnect user.  Is there a way to log off user?
Thanks. 
Avatar of Arikkan
Arikkan
Flag of United States of America image

ASKER

Nevermind, found that we can do this:
        [DllImport("wtsapi32.dll", SetLastError = true)]
        static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo