0

Situation: I am creating a file, I want access to be restricted. The only access rights that should be granted are:

  • Creator: Full access
  • Everyone else: No access

This is a windows console application.

Current code:

string secretFile = string.Format("{0}_secret.txt", System.Security.Principal.WindowsIdentity.GetCurrent().Name);
if(!File.Exists(secretFile))
{
    File.Create(secretFile);
    System.Security.AccessControl.FileSecurity fileSec = //What do I do here?
}
David Colwell
  • 2,450
  • 20
  • 31
  • 1
    There is a good example in documentation: https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity(v=vs.110).aspx – Jalal Sep 07 '16 at 04:47
  • Also you can get owner/creator of a file like this: `string user = System.IO.File.GetAccessControl(path).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();` from http://stackoverflow.com/questions/7445182 – Jalal Sep 07 '16 at 04:51
  • I may be misreading the documentation, but when I create a file, it will inherit the access control from its parent. Does this mean I need to find all those ACLs and remove them, then add one for the current user? – David Colwell Sep 07 '16 at 04:57
  • 1
    Yes probably. Look at this: http://stackoverflow.com/a/5398398/375958 – Jalal Sep 07 '16 at 18:18
  • That answer is really helpful, because it shows how to go about problem solving it for yourself. Thanks Jalal, i'll attempt it and let you know how it goes. – David Colwell Sep 07 '16 at 22:29

0 Answers0