public static void GrantFullControlOnDirectory(string path) { int takeownExitCode = SystemUtils.RunProcessSynchronouslyWithConsoleOutput("takeown", $"/F {path}"); if (takeownExitCode != 0) { throw new SecurityException($"Could not take ownership on directory {path}."); } DirectorySecurity directoryAcl = Directory.GetAccessControl(path); directoryAcl.AddAccessRule(new FileSystemAccessRule( WindowsIdentity.GetCurrent().User, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow )); Directory.SetAccessControl(path, directoryAcl); }
public static void GrantFullControlOnFile(string path) { int takeownExitCode = SystemUtils.RunProcessSynchronouslyWithConsoleOutput("takeown", $"/F {path}"); if (takeownExitCode != 0) { throw new SecurityException($"Could not take ownership on file {path}."); } FileSecurity fileAcl = File.GetAccessControl(path); fileAcl.AddAccessRule(new FileSystemAccessRule( WindowsIdentity.GetCurrent().User, FileSystemRights.FullControl, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow )); File.SetAccessControl(path, fileAcl); }
/** * Removes the specified component using install-wim-tweak synchronously * Messages from install-wim-tweak process are printed asynchronously (as soon as they are written to stdout/stderr) */ public static void RemoveComponentUsingInstallWimTweakIfAllowed(string component) { if (!Configuration.Instance.AllowInstallWimTweak) { ConsoleUtils.WriteLine($"Skipped removal of component {component} using install-wim-tweak since " + @"option ""AllowInstallWimTweak"" is set to false.", ConsoleColor.DarkYellow); return; } Console.WriteLine($"Running install-wim-tweak to remove {component}..."); int installWimTweakExitCode = SystemUtils.RunProcessSynchronouslyWithConsoleOutput(Program.InstallWimTweakPath, $"/o /c {component} /r"); if (installWimTweakExitCode == 0) { Console.WriteLine("Install-wim-tweak executed successfully!"); } else { ConsoleUtils.WriteLine($"An error occurred during the removal of {component}: " + "install-wim-tweak exited with a non-zero status.", ConsoleColor.Red); } }