devxlogo

Getting the Everyone Local Group Account Name in C#

Getting the Everyone Local Group Account Name in C#

It is quite easy to get the “Everyone” account name, with the help of the DirectorySecurity and SecurityIdentifier classes. Here is a small example of getting the name and allowing the user to save a file inside a location with the correct permissions:

string PATH = "";            if (!Directory.Exists(Path.GetFullPath(PATH)))            {                Directory.CreateDirectory(PATH);                DirectorySecurity dirSec = Directory.GetAccessControl(PATH);                SecurityIdentifier siEveryobody = new SecurityIdentifier(WellKnownSidType.WorldSid, null);                dirSec.AddAccessRule(new FileSystemAccessRule(siEveryobody, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));                Directory.SetAccessControl(PATH, dirSec);            }            using (StreamWriter swWriter = new StreamWriter(PATH, true))            {                sw.WriteLine("TEXT");                sw.Flush();                sw.Close();            }

devx-admin

Share the Post: