const
string
USER_NAME =
"NewAdmin"
;
char
[] aPWchars = {
'P'
,
'a'
,
's'
,
's'
,
'w'
,
'o'
,
'r'
,
'd'
};
System.Security.SecureString oPW =
new
System.Security.SecureString();
foreach
(
char
cChr
in
aPWchars) {
oPW.AppendChar(cChr);
}
DirectoryEntry oComputer =
new
DirectoryEntry(
"WinNT://"
+ Environment.MachineName +
",computer"
);
DirectoryEntry oNewUser = oComputer.Children.Add(USER_NAME,
"user"
);
IntPtr pString = IntPtr.Zero;
pString = Marshal.SecureStringToGlobalAllocUnicode(oPW);
oNewUser.Invoke(
"SetPassword"
,
new
object
[] { Marshal.PtrToStringUni(pString) });
oNewUser.Invoke(
"Put"
,
new
object
[] {
"Description"
,
"New Administrator"
});
oNewUser.CommitChanges();
Marshal.ZeroFreeGlobalAllocUnicode(pString);
DirectoryEntry oGroup = oComputer.Children.Find(
"Administrators"
,
"group"
);
oGroup.Invoke(
"Add"
,
new
object
[] { oNewUser.Path.ToString() });