我有一个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
更多推荐
发布评论