2
我可以很好地添加用戶,但是無法將其添加到本地組。我得到這個錯誤: -將本地用戶添加到C#中的本地組中
A member could not be added to or removed from the local group because the member does not exist.
這是我正在使用的代碼。我做錯了什麼?這只是本地機器,我絕對有權這樣做,並且該組明確存在。
try
{
using (DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://" + serverName))
{
DirectoryEntries entries = hostMachineDirectory.Children;
foreach (DirectoryEntry entry in entries)
{
if (entry.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
{
// Update password
entry.Invoke("SetPassword", password);
entry.CommitChanges();
return true;
}
}
DirectoryEntry obUser = entries.Add(userName, "User");
obUser.Properties["FullName"].Add("Used to allow users to login to Horizon. User created programmatically.");
obUser.Invoke("SetPassword", password);
obUser.Invoke("Put", new object[] {
"UserFlags",
0x10000
});
obUser.CommitChanges();
foreach (string group in groups)
{
DirectoryEntry grp = hostMachineDirectory.Children.Find(group, "group");
if (grp != null) { grp.Invoke("Add", new object[] { obUser.Path.ToString() }); }
}
return true;
}
}
catch (Exception ex)
{
returnMessage = ex.InnerException.Message;
return false;
}
它的工作原理!非常感謝。 – nickthompson
不錯。記住將它標記爲答案呢?我可以使用這些觀點,所以每個人都認爲我比實際上更知情。 P – PeteH
救命恩,謝謝! +1 –