10

プログラムでローカルユーザーグループを作成する方法を探しています。ユーザーのクエリと追加の方法についてはたくさんの例を見つけましたが、新しいグループを作成する方法については何も理解できません。

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

新しいローカルグループを作成するために使用できるコードスニペットを持っている人はいますか?

4

2 に答える 2

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();

ユーザーに関するこの記事から引用。

例でInvoke"Put"を見逃したようです。これが、NotImplementedExceptionが表示されている理由だと思います。

于 2010-07-01T09:46:30.313 に答える
7

次のことを試すことができます(自分で試したことはありません)。

PrincipalContext context = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(context);
group.Name = model.Name;
group.Save();

これはを使用しSystem.DirectoryServices.AccountManagementます。

于 2010-07-01T09:47:10.763 に答える