如何创建本地用户组(C#)
我正在寻找一种以编程方式创建本地用户组的方法。我找到了大量有关如何查询和添加用户的示例,但我无法理解如何创建新组。
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
有人有可以用来创建新本地组的代码片段吗?
I'm looking for a way how to programmatically create a local user group. I found plenty of examples on how to query and add users but nothing I can understand about how to create a new group.
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();
}
This is what I've arrived at but I know it's wrong as CommitChanges()
just throws a NotImplementedException
.
I've been using this as a sample but I can't even get it to work (thanks MS):
http://msdn.microsoft.com/en-us/library/ms815734
Anyone have a code snippet I can use to create a new local group?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
改编自这篇有关用户的文章。
看起来您错过了示例中的 Invoke“Put” - 我想这就是您看到 NotImplementedException 的原因。
This works for me:
Adapted from this article on users.
It looks like you missed the Invoke "Put" in your example - I guess this is why you are seeing the NotImplementedException.
您可以尝试以下操作(我自己没有尝试过):
这使用
System.DirectoryServices.AccountManagement
。You may try the following (haven't tried it myself):
This uses
System.DirectoryServices.AccountManagement
.