为什么在写入StreamWriter时会收到System.Collections.Generic.Dictionary`2?

时间:2014-01-23 06:50:09

标签: c# dictionary streamwriter textwriter

我有这段代码:

        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();
        }
    }
}

但输出仅为System.Collections.Generic.Dictionary2[System.String,System.String] 而不是用户的组名。

有人知道问题是什么吗?

P.S此代码应该是本地的。

1 个答案:

答案 0 :(得分:2)

变量LocalGroupsDictionary<string, string>

您不能简单地将其写入TextWriter。当你尝试时,你会得到对象的类型,这就是你所看到的。

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

相反,您可以遍历集合并编写每个键/值对:

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