I am trying to see if a windows user account is logged in or not. Here is my current code:
Dictionary userMap = new Dictionary ();
private void popUserList()
{
ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount");
ManagementObjectCollection users = usersSearcher.Get();
var localUsers = users.Cast().Where(
u => (bool)u["LocalAccount"] == true &&
int.Parse(u["SIDType"].ToString()) == 1 &&
u["Name"].ToString() != "HomeGroupUser$"
);
userMap.Clear();
userAccountLst.Items.Clear();
foreach (ManagementObject user in localUsers)
{
userMap[user["Name"].ToString()] = user;
userAccountLst.Items.Add(user["Name"].ToString());
}
}
private ManagementObject getCurrentUser()
{
return userMap[userAccountLst.SelectedItem.ToString()];
}
private bool IsUserLoggedIn(ManagementObject user)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserProfile WHERE Loaded = True");
foreach (ManagementObject obj in searcher.Get())
{
MessageBox.Show(obj["Antecedent"].ToString());
if (obj["Antecedent"].ToString().Contains(getCurrentUser()["Name"].ToString()))
{
return true;
}
}
return false;
}I have a WinForms application that has a Listbox with all the user accounts list in it, for context.
However, unbeknownst to me, Win32_UserProfile doesn't work anymore. It's not anywhere in Microsoft's documentation. Assumingly deprecated. I've been looking around Google for hours trying to get a method that is not:
a. Getting if a user account is logged in on ASP.NET.
b. Getting if a user account is logged in via Remote Connection.
c. Getting what the CURRENT user account is.
I just want to input a user as a ManagementObject, and get if it is logged into Windows LOCALLY.

Prasad RaveendranRank #320 · 7.8K reputationPosted Sep 22, 2023, 6:30 PM EDT
try the below code and see if this works for you.
Dictionary userMap = new Dictionary(); private void popUserList() { try { using (ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount")) using (ManagementObjectCollection users = usersSearcher.Get()) { var localUsers = users.Cast().Where( u => (bool)u["LocalAccount"] == true && int.Parse(u["SIDType"].ToString()) == 1 && u["Name"].ToString() != "HomeGroupUser$" ); userMap.Clear(); userAccountLst.Items.Clear(); foreach (ManagementObject user in localUsers) { userMap[user["Name"].ToString()] = user; userAccountLst.Items.Add(user["Name"].ToString()); } } } catch (Exception ex) { // Handle any exceptions that may occur during the WMI query. MessageBox.Show("Error while populating user list: " + ex.Message); } } private ManagementObject getCurrentUser() { string selectedUser = userAccountLst.SelectedItem?.ToString(); if (!string.IsNullOrEmpty(selectedUser) && userMap.ContainsKey(selectedUser)) { return userMap[selectedUser]; } return null; } private bool IsUserLoggedIn(ManagementObject user) { if (user == null) { return false; // User not selected or not found in userMap. } try { using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserProfile WHERE Loaded = True")) { foreach (ManagementObject obj in searcher.Get()) { string antecedent = obj["Antecedent"].ToString(); if (antecedent.Contains(user["Name"].ToString())) { return true; } } } } catch (Exception ex) { // Handle any exceptions that may occur during the WMI query. MessageBox.Show("Error while checking user login status: " + ex.Message); } return false; }Please note that checking user login status based on
Win32_UserProfilemay not always provide reliable results due to differences in system behavior and configurations.Ajay KumarRank #56 · 29K reputationPosted Sep 23, 2023, 4:22 AM EDT
using System; using System.Diagnostics; using System.Linq; using System.Windows.Forms; namespace UserLoginChecker { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { popUserList(); } private void popUserList() { userAccountLst.Items.Clear(); // Get the list of all currently logged-in users var loggedInUsers = GetLoggedInUsers(); foreach (var user in loggedInUsers) { userAccountLst.Items.Add(user); } } private string[] GetLoggedInUsers() { // Get the list of all currently running processes var processes = Process.GetProcesses(); // Filter the list to unique user names var loggedInUsers = processes .Where(p => !string.IsNullOrEmpty(p.StartInfo.UserName)) .Select(p => p.StartInfo.UserName) .Distinct() .ToArray(); return loggedInUsers; } private void userAccountLst_SelectedIndexChanged(object sender, EventArgs e) { // Check if the selected user is still logged in var selectedUser = userAccountLst.SelectedItem.ToString(); bool isLoggedIn = IsUserLoggedIn(selectedUser); if (isLoggedIn) { MessageBox.Show($"{selectedUser} is currently logged in."); } else { MessageBox.Show($"{selectedUser} is not logged in."); } } private bool IsUserLoggedIn(string username) { // Check if the specified username is in the list of logged-in users var loggedInUsers = GetLoggedInUsers(); return loggedInUsers.Contains(username, StringComparer.OrdinalIgnoreCase); } } }