The*_*d Z 10 c# local usergroups user-management
我正在寻找一种如何以编程方式创建本地用户组的方法.我发现了大量关于如何查询和添加用户的示例,但我无法理解如何创建新组.
var dirEntry = new DirectoryEntry(
"WinNT://" + Environment.MachineName + ",computer");
/* Code to test if the group already exists */
if (!found)
{
DirectoryEntry grp = dirEntry.Children.Add(groupName, "Group");
dirEntry.CommitChanges();
}
Run Code Online (Sandbox Code Playgroud)
这就是我所得到的,但我知道这是错误的,CommitChanges()只是抛出一个NotImplementedException.
我一直在使用它作为样本,但我甚至无法让它工作(感谢MS):
http://msdn.microsoft.com/en-us/library/ms815734
任何人都有一个我可以用来创建一个新的本地组的代码片段?
Rob*_*ine 10
这对我有用:
var ad = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry newGroup = ad.Children.Add("TestGroup1", "group");
newGroup.Invoke("Put", new object[] { "Description", "Test Group from .NET" });
newGroup.CommitChanges();
Run Code Online (Sandbox Code Playgroud)
改编自这篇文章的用户.
看起来你错过了你的例子中的Invoke"Put" - 我想这就是你看到NotImplementedException的原因.
您可以尝试以下方法(我自己没试过):
PrincipalContext context = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(context);
group.Name = model.Name;
group.Save();
Run Code Online (Sandbox Code Playgroud)
这用System.DirectoryServices.AccountManagement.