gpt4 book ai didi

c# - 为什么我在写入 StreamWriter 时得到 System.Collections.Generic.Dictionary`2?

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:23 25 4
gpt4 key购买 nike

我有这个代码:

        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 这个代码应该是本地的。

最佳答案

变量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);

关于c# - 为什么我在写入 StreamWriter 时得到 System.Collections.Generic.Dictionary`2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301284/

25 4 0
文章推荐: c# - 使用 ImageBrush 启动时程序崩溃
文章推荐: mysql - 无法在Mavericks上启动MySQL
文章推荐: mysql - where语句在存储过程中不起作用
文章推荐: c# - 将成员序列化为 Object 或 List