代码创建用户并建立权限(Code To Create a User and Establish Permissions)

编程入门 行业动态 更新时间:2024-10-25 00:34:33
代码创建用户并建立权限(Code To Create a User and Establish Permissions)

我有一个C#windows服务应用程序,它生成一个写入C:\ Program Files \ Company \ Product \ log.txt的日志文件。 我使用的是Visual Studio 2010提供的安装程序,需要安装程序: 1.创建名为ProductUser的用户 2.设置C:\ Program Files \ Company \ Product \的权限,以允许ProductUser写入目录。

I have a C# windows service application that generates a log file that writes to C:\Program 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.

最满意答案

不是最好的解决方案......但只是一个想法:如何运行命令net user password username / 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()); } }

这不是我的:这是链接 。 请让我知道这对你有没有用

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

更多推荐

本文发布于:2023-07-27 15:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292405.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权限   代码   用户   Code   Permissions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!