Created
April 1, 2014 14:22
-
-
Save lomedil/9915109 to your computer and use it in GitHub Desktop.
Grant file acceso to Everyone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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