Click here to Skip to main content
14,937,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below are two sections in C#.
One: supposedly, returns ALL current windows platform usernames.

Two: supposedly, returns ALL active logged in windows users. whether one and/or many.

But yet, I'm trying to tye it down to the localhost as seen below. No hardcoding.
I cannot get section two to work. I've tried many different ways to achieve my goal of
listing ALL active logged in windows users. whether one and/or many. Can someone
also return if each windows user has admin-prov or not.

Is there some knowledgible person who could rework the code to make it better and achieve the goals.

Thanks, in advance. Member 15028314
...

What I have tried:

There is what I have done...

C#
string userlabel1 = System.Environment.UserName;            // is there a query to retrieve computer name. 
            currentuserlabel1.Text = userlabel1.ToString();

            var connectionOptions = new ConnectionOptions();
            ManagementScope xscope = new System.Management.ManagementScope("\\\\localhost", connectionOptions);  // <-- do I need this statement on each query? 

// section one.  
// get ALL usernames on the windows local platform.

           SelectQuery query = new SelectQuery("Win32_UserAccount");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);           //  (xscope, query);
            foreach (ManagementObject envVar in searcher.Get())
            {
                tline = (string)envVar["Status"];
                if (tline.Contains("OK"))
                {
                    listBox1.Items.Add(envVar["Name"]);
                    icnt = icnt + 1;
                }
              }
            listBox1.SelectedIndex = 0;
            listBox1.Refresh();
            xlabel1.Text = "";
            xlabel1.Text = icnt.ToString();
            ////==============================
            string userlabel1 = System.Environment.UserName;
            currentuserlabel1.Text = userlabel1.ToString();

// section two  
// get ALL Log'ed in usernames on the windows local platform.

           var connectionOptions = new ConnectionOptions();
            ManagementScope xscope = new System.Management.ManagementScope("\\\\localhost", connectionOptions);
            ObjectQuery xquery = new System.Management.ObjectQuery("select * from Win32_ComputerSystem");
            ManagementObjectSearcher xsearcher = new ManagementObjectSearcher(xscope, xquery);
            foreach (var row in xsearcher.Get())
            {
               String tlinex = (String)row["Name"].ToString();
                Console.WriteLine(row["Userame"].ToString());
            }
Posted
Updated 28-Dec-20 4:48am
v3
Comments
Gerry Schmitz 27-Dec-20 23:05pm
   
Environment.MachineName.

1 solution

Where to begin...

First, a pet peeve of mine. WHY ARE YOU CALLING .ToString() ON A STRING???????? It's already a string.

You don't really need ConnectionOptions for such a simple query on a local machine.

...and you mispelled "UserName" in the last Console.WriteLine statement.

A slightly rewritten version of your code:
C#
string userNameEnv = System.Environment.UserName;
Console.WriteLine(userNameEnv);

// The path for a management scope on the local machine
// in the namespace Win32_ComputerSystem can be found.
ManagementScope scope = new ManagementScope(@"\\.\root\CIMV2");
scope.Connect();

// Query for all the Win32_ComputerSystem instances. Normally,
// this is just 1 instance, but it is still returned in a
// collection.
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

// Tell the searcher to go do its thing and return the result collection.
ManagementObjectCollection results = searcher.Get();

// Output the result of the search.
foreach (var item in results)
{
    Console.WriteLine($"Computer Name: {item["Name"]}\r\nLogged In User: {item["UserName"]}");
}
   
Comments
Maciej Los 28-Dec-20 3:05am
   
5ed!
BillWoodruff 28-Dec-20 5:28am
   
+5

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