开发者

Code To Create a User and Establish Permissions

I have a C# windows service application that generates a log file that writes to C:\Program 开发者_JAVA百科Files\Company\Product\log.txt. I am using the installer provided by Visual Studio 2010 and need to have the installer:

1. Create a user named ProductUser

2. Set permission for C:\Program Files\Company\Product\ to allow ProductUser to write to the directory.


Not the best solution... but only an idea: what about running the command net user password username /ADD? If you can do that, I can find an old vbs I used on a server to grant permissions to user on a folder.

OK, I googled a little bit and found this:

public void CreateUserAccount(string login , string password , string fullName, bool isAdmin)
{
    try
    {
        DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
        DirectoryEntries entries = dirEntry.Children;
        DirectoryEntry newUser  = entries.Add (login, "user");
        newUser.Properties["FullName"].Add(fullName);
        newUser.Invoke("SetPassword", password);
        newUser.CommitChanges();

        // Remove the if condition along with the else to create user account in "user" group.
        DirectoryEntry grp;
        if (isAdmin)
        {
            grp = dirEntry.Children.Find("Administrators", "group");
            if (grp != null) {grp.Invoke("Add", new object[] {newUser.Path.ToString()});}
        }
        else
        {
            grp = dirEntry.Children.Find("Guests", "group");
            if (grp != null) {grp.Invoke("Add", new object[] {newUser.Path.ToString()});}
        }

    }
    catch (Exception ex)
    {
            MessageBox.Show(ex.ToString());
    }
}

It's not mine: here is the link. Let me know if it works for you

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜