我想使用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");
}发布于 2012-03-08 08:21:04
您正在重定向CACLS流程的标准输入。
因此,您需要用您的输入(如stream.Writeline(“Y”))向流程提供信息;
看看这个MSDN样品
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.Writeline("Y"); // Could it be localized ??? -->Oui, Ja, Sì etc...
myStreamWriter.Close(); // This seems to be is important.....发布于 2012-03-08 08:23:12
不要调用命令行cacls实用程序,而是使用.NET API来更改权限。调用命令行实用程序应该始终被视为执行任务的最后解决办法。直接访问用于编程访问的API要好得多。
Directory.GetAccessControl(string)获取当前ACL。Directory.SetAccessControl(string, DirectorySecurity)设置ACL。通常,在使用all时,最好只使用授予权限,而完全不使用拒绝标志。拒绝BUILTIN\Users是非常广泛的,这将否决任何授予任何用户。最好是构造一个ACL,该ACL不向普通用户授予任何权限,而只授予应该具有访问权限的特定用户。
https://stackoverflow.com/questions/9614561
复制相似问题