Skip to content

Instantly share code, notes, and snippets.

@lomedil
Created April 1, 2014 14:22
Show Gist options
  • Save lomedil/9915109 to your computer and use it in GitHub Desktop.
Save lomedil/9915109 to your computer and use it in GitHub Desktop.
Grant file acceso to Everyone
private static void GrantAccess(string filepath)
{
// Get "Everyone" account name
var sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var acct = sid.Translate(typeof(NTAccount)) as NTAccount;
var everyoneAccountName = acct.ToString(); // SP=Todos, EN=Everyone, FR...
try
{
var infoFile = new FileInfo(filepath);
// Create file if it does not exists yet
if (!infoFile.Exists)
infoFile.Create();
var fileAccesoControl = infoFile.GetAccessControl();
fileAccessControl.AddAccessRule(
new FileSystemAccessRule(
everyoneAccountName,
FileSystemRights.FullControl,
AccessControlType.Allow
)
);
infoFile.SetAccessControl(fileAccessControl);
}
catch (Exception)
{ }
finally
{
sid = null; acct = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment