Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to programmatically create Windows user accounts on Windows 7 or Windows Server 2008?
    text
    copied!<p>I've been trying to create new local user accounts on windows 7 machine. I used the System.DirectoryServices.DirectoryEntry class (as in <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;306273">here</a>) but it doesn't seem to work. </p> <p>Here's the code in the article:</p> <pre><code>static void Main(string[] args) { try { DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user"); NewUser.Invoke("SetPassword", new object[] {"#12345Abc"}); NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"}); NewUser.CommitChanges(); DirectoryEntry grp; grp = AD.Children.Find("Guests", "group"); if (grp != null) {grp.Invoke("Add", new object[] {NewUser.Path.ToString()});} Console.WriteLine("Account Created Successfully"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } </code></pre> <p>When executing this line </p> <p><code>DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");</code> </p> <p>I get a </p> <p><code>System.Runtime.InteropServices.COMException</code> with "<code>{"Unknown error (0x80005000)"}</code>" </p> <p>as the exception message, and <code>-2147463168</code> as the error code.</p> <p>I assume this is probably because the article targets Windows XP and below machines, and I'm targeting windows 7 and Windows server 2008. </p> <p>Any help appreciated! <br /> <br /> <strong>Update:</strong> <br /> <em>For some mysterious reason, i'm no longer seeing that <code>System.Runtime.InteropServices.COMException</code>, however, when committing the changes here <code>newuser.CommitChanges()</code>, I get a "<code>UnAuthorizedAccessException</code>". I tried running the app as administrator, but still not working.</em></p> <p><strong>Update 2:</strong> <br /> OK, after changing to the UserPrincipal class, i got the follwoing code to work:<br /></p> <pre><code>public UserPrincipal CreateNewUser(string sUserName, string sPassword) { // first check that the user doesn't exist if (GetUser(sUserName) == null) { PrincipalContext oPrincipalContext = GetPrincipalContext(); UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext); oUserPrincipal.Name = sUserName; oUserPrincipal.SetPassword(sPassword); //User Log on Name //oUserPrincipal.UserPrincipalName = sUserName; oUserPrincipal.Save(); return oUserPrincipal; } // if it already exists, return the old user return GetUser(sUserName); } } </code></pre> <p><br /> This code runs well when I run it as a console app -of course run as administrator- but when i deployed it as a windows service, with the security account set as "LocalSystem", i get an <code>InvlaidOperationException</code> saying "The underlying store does not support this property" <br /></p> <p>Thoughts?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload