我正在寻找一种如何以编程方式创建本地用户组的方法。我找到了很多关于如何查询和添加用户的示例,但我对如何创建新组一无所知。
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();
}
这是我得出的结论,但我知道这是错误的,因为 CommitChanges()
只是抛出一个 NotImplementedException
。
我一直在使用它作为示例,但我什至无法让它工作(感谢 MS):
http://msdn.microsoft.com/en-us/library/ms815734
谁有我可以用来创建新本地组的代码片段?
最佳答案
这对我有用:
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();
改编自this关于用户的文章。
看起来您在示例中错过了 Invoke“Put”——我想这就是您看到 NotImplementedException 的原因。
关于c# - 如何创建本地用户组(在 C# 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3156551/