我正在编写一个程序,它将基本上为用户在本地计算机sam存储中生成大多数内容的XML副本。
目前,它只为每个用户打印一个xmlElement,但不打印这些用户的属性。
<?xml version="1.0" encoding="utf-8"?>
<WindowsUserList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<windowsUserEntry />
<windowsUserEntry />
...
<windowsUserEntry />
<windowsUserEntry />
</WindowsUserList>
这是我的主代码,复选框文本将搜索的名称格式化为文本(accounting*例如获取用户帐户1、2、3等)。
windowsUserList listUsers = new windowsUserList();
PrincipalContext context = new PrincipalContext(ContextType.Machine, Settings.Default.ipAddress, Settings.Default.username, Settings.Default.password);
foreach (CheckBox cbx in groupBox1.Controls.OfType<CheckBox>())
{
if (cbx.Checked)
{
UserPrincipal usr = new UserPrincipal(context);
if (cbx.Text == "")
{
usr.Name = txtCustom.Text;
}
else
{
usr.Name = cbx.Text;
}
PrincipalSearcher search = new PrincipalSearcher(usr);
PrincipalSearchResult<Principal> results = search.FindAll();
foreach (Principal result in results)
{
listUsers.AddUser(new windowsUserEntry((UserPrincipal)result));
} // foreach (Principal result in results)
}//if (cbx.Checked)
}//foreach (CheckBox cbx in groupBox1.Controls.OfType<CheckBox>())
XmlSerializer s = new XmlSerializer(typeof(windowsUserList));
TextWriter w = new StreamWriter(dlgSave.OpenFile());
s.Serialize(w, listUsers);
w.Close();
Windows用户列表/条目的代码很长,因此我将其发布到
pastebin