Search code examples
c#.netarrayslistcassia

C# Add server users to List<>


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.


Solution

  • Try printing out your list using following

    list.ForEach(Console.WriteLine);
    

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