如何通过C#在文件夹上添加本地服务权限

时间:2015-04-21 19:26:44

标签: c# windows-services

我找到了如何通过C#以编程方式在C:\Program Files的文件夹中添加本地服务权限,并从此网址http://stackoverflow.com/questions/5298905/add-everyone-privilege-to-folder-using-c-net/5398398#5398398

进行了编写

他们会向everyone用户展示如何执行此操作。

DirectorySecurity sec = Directory.GetAccessControl(path);
// Using this instead of the "Everyone" string means we work on non-English systems.
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(path, sec);

告诉我在上面的代码中需要更改的内容,因此local service权限将添加到特定文件夹中。

我开发了一个Windows服务,它将在运行时在其中创建一个文件夹和xml文件。当我从安装文件安装我的服务时,没有创建文件夹,但也没有错误返回。

所以我调试服务,看到它可以在调试时创建文件夹和xml文件。从安装文件安装服务时出现问题。我无法捕捉问题,例如发生了什么问题。因此,指导我应该怎样做才能捕获问题,例如"为什么我的服务无法创建文件夹和文件"寻求指导。感谢

1 个答案:

答案 0 :(得分:3)

new SecurityIdentifier(WellKnownSidType.LocalServiceSid, null);

new SecurityIdentifier("S-1-5-19");