C# 命令行 SetACL

发布于 2024-10-20 18:48:16 字数 693 浏览 2 评论 0原文

我正在尝试解决文件夹所有者的问题。我正在使用 SetACL。我可以使用 cmd 并使参数起作用,但是当我尝试将其添加到程序中时......它不起作用。我设置了一个断点来确保参数正确传递,确实如此。欢迎任何帮助。

        Process p = new Process();

        if (Wow.Is64BitOperatingSystem == true)
        {
            p.StartInfo.FileName = "SetACLx64.exe";
        }
        else
        {
            p.StartInfo.FileName = "SetACLx86.exe";
        }

        string command = @" -on """ + path +
            @""" -ot file -actn setprot -op ""dacl:np;sacl:nc"" -actn setowner -ownr ""n:" + account + @";"" -rec cont_obj";
        p.StartInfo.Arguments = command;
        p.Start();

我已经让它在同一个程序中正常工作以解决注册表问题。只是无法让这个例子发挥作用。我尝试设置的文件夹是 %temp% 文件夹。

I'm trying to fix an issue with the owner on a folder. I am using SetACL. I can use cmd and make the arguments work, but when I try adding it to a program...it doesn't work. I've set a break point to ensure the argument is passed right and it was. Any help is welcome.

        Process p = new Process();

        if (Wow.Is64BitOperatingSystem == true)
        {
            p.StartInfo.FileName = "SetACLx64.exe";
        }
        else
        {
            p.StartInfo.FileName = "SetACLx86.exe";
        }

        string command = @" -on """ + path +
            @""" -ot file -actn setprot -op ""dacl:np;sacl:nc"" -actn setowner -ownr ""n:" + account + @";"" -rec cont_obj";
        p.StartInfo.Arguments = command;
        p.Start();

I have got this to work in the same program for a registry issue without trouble. Just can't get this example to work. Folder I'm try to set is the %temp% folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聆听风音 2024-10-27 18:48:16

如果它按照 Sanjeevakumar 的要求以管理员身份运行,则

尝试删除命令变量中的第一个空格。 Arguments 参数不需要您为参数提供初始空间。可能这会导致问题。

还可以尝试通过在调用 Start() 方法之前添加以下行来利用进程的错误数据。

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataHandler);

然后定义事件处理程序。

private static void ErrorDataHandler(object sendingProcess, DataReceivedEventArgs e)
{
  //using the DataReceivedEventArgs see if there is an error.
  //If it comes there there is most likely an error.
}

If it is running as admin as Sanjeevakumar asked then

Try removing the first space in your command variable. The Arguments parameter does not require that you provide an initial space for the arguments. May be that causes the problem.

Also try tapping into the error data of your process by adding the following lines before calling the Start() method.

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataHandler);

And then define the event handler.

private static void ErrorDataHandler(object sendingProcess, DataReceivedEventArgs e)
{
  //using the DataReceivedEventArgs see if there is an error.
  //If it comes there there is most likely an error.
}
涫野音 2024-10-27 18:48:16

那么当路径为“%temp%”时,您的代码不起作用?在这种情况下,解决方案很简单:变量扩展不是由 SetACL 完成的,而是由 SetACL 启动之前的命令 shell 完成的。如果直接启动 SetACL 而不调用 cmd.exe,则永远不会发生变量扩展。

您有两个选择:

  1. 在 C# 代码中使用 Environment.GetEnvironmentVariable< 展开“%temp%” /a>.
  2. 通过 cmd 调用 SetACL,如下所示: cmd /c SetACL -on %temp% -ot file ...

So your code does not work when path is "%temp%"? In that case the solution is simple: variable expansion is not done by SetACL but the command shell before SetACL is even started. If you start SetACL directly without invoking cmd.exe then variable expansion never takes place.

You have two options:

  1. Expand "%temp%" in C# code with Environment.GetEnvironmentVariable.
  2. Call SetACL via cmd like this: cmd /c SetACL -on %temp% -ot file ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文