adam2009 0 Newbie Poster

Hi,

I have got some questions. I hope someone could help me out.

1. I have wrote two methods in c# that manages local users in WinServer2008: adding local user and removing local user.
Adding a local user work just fine. I have encountered some problems removing that user afterwards.

I have received some exceptions like:

*"cannot perform this operation on built-in accounts"
the last exception I have received, when I have used

DirectoryEntry userEntry = dirEntry.Children.Find(userName, "user");
children.Remove(groupEntry);

a small debugging shows more details:
http://drop.io/hidden/eopb4tphk8qekl/asset/ZXJyb3ItcmVtb3ZpbmctanBn

*"exception has been thrown by the target of the invocation"
when I have used

groupEntry.Invoke("Remove", new object[] { userEntry.Path.ToString() });
 groupEntry.CommitChanges();

2. A question arise during coding: is winNT a protocol (alains with windows OS avoid me from finding a proper answer in google) ?

3. I'm looking for c# code that configure WindowsServer2008 number of concurrent local users to some given N (more than 2 which is the default).


I'm sorry for the shower of questions. I appreciate any help

My code:

public static void addUser(string userName)
        {
            try
            {
                System.Console.WriteLine("Creating user {0} ", userName);
                DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry NewUser = AD.Children.Add(userName , "user");
                System.Console.WriteLine("path is {0} ", AD.Path);
                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();

            }
        }


        public static void removeUser(string userName)
   {
       try
       {
           System.Console.WriteLine("Removing user {0} ", userName);
           string path = string.Format("WinNT://{0},computer", Environment.MachineName);
           using (DirectoryEntry dirEntry = new DirectoryEntry(path))
           {
               DirectoryEntry groupEntry = dirEntry.Children.Find("Users", "group");
               if (groupEntry != null)
               {

                   DirectoryEntry userEntry = dirEntry.Children.Find(userName, "user");
                   groupEntry.Invoke("Remove", new object[] { userEntry.Path.ToString() });
                   groupEntry.CommitChanges();
                   userEntry.Dispose();
               }
                  groupEntry.Dispose();

               Console.WriteLine("Account Removed Successfully");
           }
       }
       catch (Exception ex)
       {
           Console.WriteLine(ex.Message);
           Console.ReadLine();

       }
      }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.