Creating local user account c# and .NET 2.0

前端 未结 3 1096
说谎
说谎 2020-12-01 03:28

How can I create a local user account using .NET 2.0 and c# and also be able to set the \"Password never expires\" to never.

I have tried using \"Net.exe\" using Pr

3条回答
  •  悲&欢浪女
    2020-12-01 03:45

    using System.DirectoryServices;

        DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost");
        DirectoryEntries entries = hostMachineDirectory.Children;
        bool userExists = false;
        foreach (DirectoryEntry each in entries)
        {
            userExists = each.Name.Equals("NewUser",  
            StringComparison.CurrentCultureIgnoreCase);
            if (systemtestUserExists)
                break;
        }
    
        if (false == userExists)
        {
            DirectoryEntry obUser = entries.Add("NewUser", "User");
            obUser.Properties["FullName"].Add("Local user");
            obUser.Invoke("SetPassword", "abcdefg12345@");
            obUser.Invoke("Put", new object[] {"UserFlags", 0x10000});
            obUser.CommitChanges();
    

提交回复
热议问题