lan*_*tha 1 .net c# service cacls c#-2.0
我想使用c#服务阻止来自用户的某些文件夹.这意味着当您启动服务时,虽然它被阻止,但最终结果是阻止文件夹.我写了一个代码.但你的回答是肯定的吗?请帮忙解决这个问题
string Pathname = folder;
EventLog.WriteEntry(Pathname);
string Username = @"BUILTIN\Users";
string UserRights = "N";
if (Pathname == null || Pathname == "")
{
    EventLog.WriteEntry("No File");
}
string CommandLine = '"' + System.IO.Path.GetFullPath(Pathname) + '"' + " /C ";
CommandLine += @" /T ";
CommandLine += @" /P " + Username + ":" + UserRights;
Process p = new Process();
p.StartInfo.FileName = "cacls.exe";
p.StartInfo.Arguments = CommandLine;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.StartInfo.CreateNoWindow = true;
p.Start();
string Response = p.StandardOutput.ReadToEnd();
EventLog.WriteEntry(Response);
if (Response == null || Response == "")
{
    EventLog.WriteEntry("Error");
}
不要调用命令行cacls实用程序,而是使用.NET API更改权限.应始终将调用命令行实用程序视为执行任务的最后一种解决方法.最好直接访问用于编程访问的API.
Directory.GetAccessControl(string) 获取当前ACL.Directory.SetAccessControl(string, DirectorySecurity) 设置ACL.通常在使用ACL时,最好只使用授予权限而不使用拒绝标志.拒绝BUILTIN\Users是非常广泛的,否认将否决对任何用户的任何授权.最好构造一个ACL,该ACL不向普通用户授予任何权限,只授予应该具有访问权限的特定用户的权限.
| 归档时间: | 
 | 
| 查看次数: | 2604 次 | 
| 最近记录: |