c# - 如何创建本地用户组(在 C# 中)

标签 c# local user-management usergroups

我正在寻找一种如何以编程方式创建本地用户组的方法。我找到了很多关于如何查询和添加用户的示例,但我对如何创建新组一无所知。

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/

相关文章:

python - 连接本地网络中的另一台计算机

java - 从 java 应用程序中获取用户名 (windows)

c# - 如何在与 C# 相同的 gRPC 中声明小数对象

linux - struct stat 时间是 GMT 吗?

c# - GridView 查询不显示

javascript - 托管站点缺少本地 html 文件中的元素

java - Apache Shiro 中的用户管理

linux - 在 Windows 10 中模拟启用类似 Linux 的管理员权限

c# - System.String ElementAt[String](System.Collections.Generic.IEnumerable`1[System.String], Int32)' 方法

c# - 在 C# 中打印格式化的 HTML 页面