Why do I get System.Collections.Generic.Dictionary`2 when writing to a StreamWriter?

30k views Asked by At

I have this code:

        Console.Title = "System Reader";
        Console.WriteLine("Enter the Domain-Address <or local>:");
        string domain = Console.ReadLine();
        Console.Clear();
        string date = DateTime.Now.ToString("yyyy-MM-dd");
        string time = DateTime.Now.ToString("HH-mm-ss");
        double count = 1;
        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "user_" + domain + "_" + date + "_" + time + ".txt");
        string ComputerName = Environment.MachineName;
        Console.Clear();
        if (domain == "local")
        {
            ObjectQuery user = new ObjectQuery("SELECT * FROM Win32_UserAccount WHERE LocalAccount = True");
            ManagementObjectSearcher userSearcher = new ManagementObjectSearcher(user);
            TextWriter tw = new StreamWriter(path);
            tw.Write("Account Type" + "\t" + "Caption" + "\t" + "Description" + "\t" + "Disabled" + "\t" + "Domain" + "\t" + "Full Name" + "\t" + "Local Account" + "\t" + "Lockout" + "\t" + "Name" + "\t" + "Password Changeable" + "\t" + "Password Expires" + "\t" + "Password Required" + "\t" + "SID" + "\t" + "SID Type" + "\t" + "Status" + "\t");
            for (int i = 0; i < 10; i++)
            {
                tw.Write("Group" + (i + 1) + "\t");
            }
            tw.WriteLine("");

            foreach (ManagementObject userObj in userSearcher.Get())
            {
                tw.WriteLine(userObj["AccountType"] + "\t" + userObj["Caption"] + "\t" + userObj["Description"] + "\t" + userObj["Disabled"] + "\t" + userObj["Domain"] + "\t" + userObj["FullName"] + "\t" + userObj["LocalAccount"] + "\t" + userObj["Lockout"] + "\t" + userObj["Name"] + "\t" + userObj["PasswordChangeable"] + "\t" + userObj["PasswordExpires"] + "\t" + userObj["PasswordRequired"] + "\t" + userObj["SID"] + "\t" + userObj["SIDType"] + "\t" + userObj["Status"]);
                Dictionary<string, string> LocalGroups = new Dictionary<string, string>();
                ManagementObjectSearcher groupSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Group WHERE LocalAccount = TRUE");
                foreach (ManagementObject groupObj in groupSearcher.Get())
                {
                    ManagementObjectSearcher linkSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_GroupUser Where GroupComponent = \"Win32_Group.Domain=\'" + ComputerName + "\',Name=\'" + groupObj["Name"] + "\'\"");
                    foreach (ManagementObject linkObj in linkSearcher.Get())
                    {
                        string UName = linkObj["PartComponent"].ToString();
                        UName = UName.Substring(UName.IndexOf("=") + 1);
                        string Domain = UName.Substring(0, UName.IndexOf((","))).Replace("\"", "");
                        UName = UName.Substring(UName.IndexOf("=") + 1).Replace("\"", "");
                        if (UName.IndexOf(userObj["Name"].ToString(), 0, StringComparison.OrdinalIgnoreCase) == 0)
                            LocalGroups.Add(groupObj["Name"].ToString(), Domain + "\\" + UName);
                        tw.WriteLine(LocalGroups);
                    }
                }
            }
            tw.Close();
            TextReader tr = new StreamReader(path);
            Console.WriteLine(tr.ReadToEnd());
            tr.Close();
            Console.Write("\nPress Enter to exit ...");
            Console.Read();
        }
    }
}

But the output is only System.Collections.Generic.Dictionary2[System.String,System.String] and not the group name from the user.

Does someone know what the problem is?

P.S this code should be for local.

1

There are 1 answers

1
Grant Winney On BEST ANSWER

The variable LocalGroups is a Dictionary<string, string>.

You cannot simply write that to your TextWriter. When you try, you get the type of object, which is what you're seeing.

Dictionary<string, string> LocalGroups = new Dictionary<string, string>();
...
...
tw.WriteLine(LocalGroups);  // writes System.Collections.Generic.Dictionary`2...

Instead, you can iterate through the collection and write each key/value pair:

foreach (var group in LocalGroups)
    tw.WriteLine("Key: {0} Value: {1}", group.Key, group.Value);