简体   繁体   English

如何在VB.NET中获得当前登录Windows 7计算机的所有用户的列表?

[英]How can I get list in VB.NET of all users currently logged in to a windows 7 computer?

I've got an application running a manufacturing process on a Windows 7 computer. 我有一个在Windows 7计算机上运行制造过程的应用程序。 The app has some time-sensitive components that sometimes run slow for a second or two. 该应用程序包含一些对时间敏感的组件,有时会运行一两秒钟。 I know there are a lot of background operations happening in the CLR and OS that can cause program delays, but I've also seen the users sometimes log in to their personal accounts to check email, etc., while my app is running. 我知道CLR和OS中发生了许多后台操作,这些操作可能会导致程序延迟,但是我也看到用户有时会在我的应用程序运行时登录其个人帐户以查看电子邮件等。 I'm sure their doing this causes some of the slow-downs. 我确定他们这样做会导致某些速度变慢。 IT says they can't block user accounts on individual computers, so I'd like to have my app text me when the system is running slow and users are logged in to their personal accounts, so I can run over there and call them out on it. IT部门说他们不能在单个计算机上阻止用户帐户,所以我想让我的应用程序在系统运行缓慢且用户登录到其个人帐户时给我发短信,以便我可以在那边运行并对其进行呼叫在上面。 Thank you. 谢谢。

This worked: 这工作:

Function GetActiveUsers() As List(Of String)
    Dim activeusers As New List(Of String)
    Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
    For Each keyname As String In userskey.GetSubKeyNames()
        Using key As RegistryKey = userskey.OpenSubKey(keyname)
            Dim userpath As String = DirectCast(key.GetValue("ProfileImagePath"), String)
            Dim username As String = System.IO.Path.GetFileNameWithoutExtension(userpath)
            Dim useractive As Integer = 0
            If key.GetValueNames.Contains("RefCount") Then useractive = DirectCast(key.GetValue("RefCount"), Integer)
            If useractive <> 0 Then activeusers.Add(username)
        End Using
    Next
    Return activeusers
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM