首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c# windows服务中使用CACLS

如何在c# windows服务中使用CACLS
EN

Stack Overflow用户
提问于 2012-03-08 08:08:56
回答 2查看 3.5K关注 0票数 1

我想使用c#服务阻止用户使用某些文件夹。这意味着当您启动服务时,不管它是否被阻塞,最终结果都是阻塞文件夹。我已经写了个密码。但是你确定是吗?请帮我解决这个问题。

代码语言:javascript
复制
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");
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-08 08:21:04

您正在重定向CACLS流程的标准输入。

因此,您需要用您的输入(如stream.Writeline(“Y”))向流程提供信息;

看看这个MSDN样品

代码语言:javascript
复制
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.Writeline("Y"); // Could it be localized ??? -->Oui, Ja, Sì etc...
myStreamWriter.Close();  // This seems to be is important.....
票数 1
EN

Stack Overflow用户

发布于 2012-03-08 08:23:12

不要调用命令行cacls实用程序,而是使用.NET API来更改权限。调用命令行实用程序应该始终被视为执行任务的最后解决办法。直接访问用于编程访问的API要好得多。

  • Directory.GetAccessControl(string)获取当前ACL。
  • Directory.SetAccessControl(string, DirectorySecurity)设置ACL。

通常,在使用all时,最好只使用授予权限,而完全不使用拒绝标志。拒绝BUILTIN\Users是非常广泛的,这将否决任何授予任何用户。最好是构造一个ACL,该ACL不向普通用户授予任何权限,而只授予应该具有访问权限的特定用户。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9614561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档