Domanda

Sto cercando un modo come creare un gruppo di utenti locale. Ho trovato un sacco di esempi su come query e aggiungere gli utenti, ma nulla che io possa capire su come creare un nuovo gruppo.

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

Questo è quello che ho arrivato a, ma so che è sbagliato, come CommitChanges() getta solo un NotImplementedException.

ho usato questo come un campione, ma non riesco nemmeno a farlo funzionare (grazie MS):

http://msdn.microsoft.com/en-us/library/ms815734

Qualcuno ha un frammento di codice che posso usare per creare un nuovo gruppo locale?

È stato utile?

Soluzione

Questo funziona per me:

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

questo articolo sugli utenti.

Sembra che vi siete persi il "put" Invoke nel tuo esempio - credo che questo è il motivo per cui si sta vedendo la NotImplementedException

.

Altri suggerimenti

E 'possibile provare quanto segue (non ho provato io stesso):

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

Questo utilizza System.DirectoryServices.AccountManagement .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top