1

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.

coderblogger
  • 84
  • 11
  • Possible duplicate of [Print list items](http://stackoverflow.com/questions/1960758/print-list-items) – Vadim Martynov Feb 11 '16 at 15:08
  • You are also creating a new list on each iteration, when normally you would want to put your list declaration outside the loop (and thus have all the elements in it after the loop is complete). – crashmstr Feb 11 '16 at 15:10
  • @crashmstr, thank you, I realised that after I posted the question. – coderblogger Feb 11 '16 at 15:49

4 Answers4

2

Try printing out your list using following

list.ForEach(Console.WriteLine);

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

Lav
  • 1,850
  • 15
  • 17
1

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);
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
0

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());
T. Yates
  • 192
  • 11
0

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.

coderblogger
  • 84
  • 11