C# .NET - how to determine if directory is writable, with or without UAC? -
i'm working on piece of software needs copy file given directory on filesystem. needs work on both uac-aware oss (vista, 7) xp. around issue of writing directory uac elevation required, app kicks off process manifest states uac required. generates prompt , copy when user confirms.
from can see, directory can have 3 different logical permission states - writeable without uac elevation, writeable uac elevation , not writeable.
my question this: given directory, how reliably determine whether current user can copy (and potentially overwrite) file directory, , if can, how determine if uac elevation required?
on xp, simple checking whether 'allow write' permission granted, on vista / 7, there directories permission isn't granted, action still possible uac.
we have method writeaccess on files, can adapt directories (directory.getaccesscontrol , on)
/// <summary> checks write access given file. /// </summary> /// <param name="filename">the filename.</param> /// <returns>true, if write access allowed, otherwise false</returns> public static bool writeaccess(string filename) { if ((file.getattributes(filename) & fileattributes.readonly) != 0) return false; // access rules of specified files (user groups , user names have access file) var rules = file.getaccesscontrol(filename).getaccessrules(true, true, typeof(system.security.principal.securityidentifier)); // identity of current user , groups user in. var groups = windowsidentity.getcurrent().groups; string sidcurrentuser = windowsidentity.getcurrent().user.value; // check if writing file explicitly denied user or group user in. if (rules.oftype<filesystemaccessrule>().any(r => (groups.contains(r.identityreference) || r.identityreference.value == sidcurrentuser) && r.accesscontroltype == accesscontroltype.deny && (r.filesystemrights & filesystemrights.writedata) == filesystemrights.writedata)) return false; // check if writing allowed return rules.oftype<filesystemaccessrule>().any(r => (groups.contains(r.identityreference) || r.identityreference.value == sidcurrentuser) && r.accesscontroltype == accesscontroltype.allow && (r.filesystemrights & filesystemrights.writedata) == filesystemrights.writedata); }
hope helps.
Comments
Post a Comment