【问题标题】:Code To Create a User and Establish Permissions创建用户并建立权限的代码
【发布时间】:2011-04-19 08:42:39
【问题描述】:
我有一个 C# Windows 服务应用程序,它生成一个写入 C:\Program Files\Company\Product\log.txt 的日志文件。我正在使用 Visual Studio 2010 提供的安装程序,需要安装程序:
1.创建一个名为ProductUser的用户
2. 为 C:\Program Files\Company\Product\ 设置权限,以允许 ProductUser 写入目录。
【问题讨论】:
标签:
c#
visual-studio-2010
permissions
installation
【解决方案1】:
不是最好的解决方案......但只是一个想法:运行命令怎么样
net user 密码用户名 /ADD?
如果你能做到这一点,我可以找到我在服务器上使用的旧 vbs 来授予用户对文件夹的权限。
好的,我google了一下,发现了这个:
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());
}
}
这不是我的:这是link。
让我知道它是否适合你