开发者

C# command line SetACL

开发者 https://www.devze.com 2023-02-15 01:10 出处:网络
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

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.F开发者_StackOverflow中文版ileName = "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.


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.
}


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 ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号