C# Add server users to List<>

421 Views Asked by At

I have created an application that checks how many users are logged into a machine and performs a count. I have used Cassia to get the usernames, however I would like to put those usernames in a list and send to my database.

I am having a problem with putting those usernames into the list. Here is my code:

static void Main(string[] args)
{        
    while (!Console.KeyAvailable)
    {
        List<string> list = new List<string>();
        string host = Dns.GetHostName();
        ITerminalServicesManager manager = new TerminalServicesManager();
        using (ITerminalServer server = manager.GetRemoteServer(host))
        {
            server.Open();
            foreach (ITerminalServicesSession session in server.GetSessions())
            {
                NTAccount account = session.UserAccount;
                if (account != null)
                {                        
                    list.Add(account.ToString());
                    Console.WriteLine(list);
                    Console.WriteLine("Count: " + list.Count);
                    Thread.Sleep(10000);
                }
            }
        }
    }
}

When I run the application, the count is performed correctly, but the list is not displayed in my application. System.Collection.Generic.List``1[System.String] is displayed instead of the list. Is there a way to place those usernames in a list? Or maybe an array would be better? Any suggestions? Thank you.

4

There are 4 best solutions below

0
On BEST ANSWER

Try printing out your list using following

list.ForEach(Console.WriteLine);

That will print all the list elements. One per line.

4
On

I see couple of issues here.

First one: place list creation and printing outisde your loop, otherwise you're creating new list for each session object and I really doubt it was intended.

Second one: you can't print the list using one WirteLine since in this cast it tries to use .ToString() method of list inherited from object and gives you result like System.Collection.Generic.List``1[System.String].

Instead you have to use another loop to traverse the list and print it:

List<string> list = new List<string>();

foreach (ITerminalServicesSession session in server.GetSessions())
{
    NTAccount account = session.UserAccount;
    if (account != null)
    {
        list.Add(account.ToString());
    }
}

foreach (var item in list)
    Console.WriteLine(item);

Console.WriteLine("Count: " + list.Count);

Thread.Sleep(10000);
0
On

This line is printing the value of list.ToString() to the console:

Console.WriteLine(list);

Your accounts are in there, but the default behavior of ToString will print information about the object's type. Try replacing that line with:

Console.WriteLine(account.ToString());
0
On

You can use this code block:

foreach (var item in list)
Console.WriteLine(item);

Console.WriteLine(list) is the same Console.WriteLine(list.ToString()). ToString() method returns system name of the List object.