Skip to content

Instantly share code, notes, and snippets.

@ststeiger
Created June 5, 2024 11:32
Show Gist options
  • Save ststeiger/64ceb533d0161361d2af65d344cb9e34 to your computer and use it in GitHub Desktop.
Save ststeiger/64ceb533d0161361d2af65d344cb9e34 to your computer and use it in GitHub Desktop.
Give full rights to everyone
namespace HubSpotClient.Trash
{
internal class FileRightsModifier
{
#if MONO_UNIX_References
static void GrantFullControlToEveryoneLinux()
{
// Specify the file path for which you want to set permissions
string filePath = "/path/to/your/file";
// Set permissions to full access for EVERYONE
// S_IRWXU: Read, write, and execute permissions for the owner (user).
// S_IRWXG: Read, write, and execute permissions for the group.
// S_IRWXO: Read, write, and execute permissions for others (everyone else).
Mono.Unix.FilePermissions permissions = Mono.Unix.FilePermissions.S_IRWXU | Mono.Unix.FilePermissions.S_IRWXG | Mono.Unix.FilePermissions.S_IRWXO;
// S_IRUSR: Read permission for the owner (user).
// S_IWUSR: Write permission for the owner (user).
// S_IXUSR: Execute permission for the owner (user).
// Change file permissions
try
{
Mono.Unix.Syscall.Chmod(filePath, permissions);
System.Console.WriteLine("Permissions changed successfully.");
}
catch (System.Exception ex)
{
System.Console.WriteLine($"Failed to change permissions: {ex.Message}");
}
}
#endif
public static void GrantFullControlToEveryone(string filePath)
{
if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
return;
if (!System.IO.File.Exists(filePath))
{
throw new System.IO.FileNotFoundException($"File not found: {filePath}");
}
// Get file security information
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
System.Security.AccessControl.FileSecurity fileSecurity = System.IO.FileSystemAclExtensions.GetAccessControl(fileInfo);
// Create a FileSystemAccessRule for Everyone with FullControl
System.Security.Principal.SecurityIdentifier everyoneSid =
new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);
System.Security.AccessControl.FileSystemAccessRule everyoneRule =
new System.Security.AccessControl.FileSystemAccessRule(everyoneSid,
System.Security.AccessControl.FileSystemRights.FullControl,
// * In most operating systems, including Windows,
// * setting access control entries (ACEs) that involve inheritance flags
// * (like ContainerInherit or ObjectInherit) requires administrative privileges.
// System.Security.AccessControl.InheritanceFlags.ContainerInherit |
// System.Security.AccessControl.InheritanceFlags.ObjectInherit
// * Without root rights, only works with none
System.Security.AccessControl.InheritanceFlags.None,
System.Security.AccessControl.PropagationFlags.NoPropagateInherit,
System.Security.AccessControl.AccessControlType.Allow
);
// Add the rule to the file security
fileSecurity.AddAccessRule(everyoneRule);
// Apply the updated security settings
System.IO.FileSystemAclExtensions.SetAccessControl(fileInfo, fileSecurity);
} // End Function GrantFullControlToEveryone
public static void GrantFullControlToEveryone()
{
string directory = @"C:\temp";
foreach (string filePath in System.IO.Directory.GetFiles(directory, "*.*", System.IO.SearchOption.TopDirectoryOnly))
{
GrantFullControlToEveryone(filePath);
} // Next filePath
} // End Sub GrantFullControlToEveryone
} // End Class FileRightsModifier
} // End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment