Create User and add it to Administrators Group While Setup a Windows Service

The Following Code is to Create User and add it to Administrator Group while setting up your windows Service.
it should be added to the Service installer Section


    Public Overloads Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
        MyBase.Install(stateSaver)


        ‘Create User and add it to Administrator Group
        Try
            Dim AD As New DirectoryEntry(“WinNT://” & Environment.MachineName & “,computer”)
            Dim NewUser As DirectoryEntry = AD.Children.Add(“UserName”, “user”)
            NewUser.Invoke(“SetPassword”, New Object() {“Password”})
            NewUser.Invoke(“Put”, New Object() {“Description”, “User Description”})
            NewUser.CommitChanges()
            Dim grp As DirectoryEntry


            grp = AD.Children.Find(“Administrators”, “group”)
            If grp IsNot Nothing Then
                grp.Invoke(“Add”, New Object() {NewUser.Path.ToString()})
            End If


        Catch ex As Exception


        End Try
    End Sub





Advertisements

Posted on January 4, 2010, in How To, VB.NET. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: