score:0

note: i haven't tested it, but in theory it should work

first, define a predicate to determine read access

bool canread(fileinfo file)
{
  try {
    file.getaccesscontrol();
    //read and write access;
    return true;
  }
  catch (unauthorizedaccessexception uae)
  {
    if (uae.message.contains("read-only"))
    {
      //read-only access
      return true;
    }
    return false;
  }
}


then, it should be a simple case of using a where clause in a linq query

from file in directoryinfo.getfiles("\\server\share\folder\") 
  where haveaccess(f) == true
    select f;

score:0

tested and working, but will return false if the file is in use

void main()
{
    var directoryinfo = new directoryinfo(@"c:\");
    var currentuser = windowsidentity.getcurrent();
    var files = directoryinfo.getfiles(".").where(f => canread(currentuser, f.fullname));
}

private bool canread(windowsidentity user, string filepath)
{
    if(!file.exists(filepath))
        return false;

    try
    {
        var filesecurity = file.getaccesscontrol(filepath, accesscontrolsections.access); 
        foreach(filesystemaccessrule fsrule in filesecurity.getaccessrules(true, true, typeof(system.security.principal.securityidentifier)))
        {
            foreach(var usrgroup in user.groups)
            {
                if(fsrule.identityreference.value == usrgroup.value)
                    return true;
            }
        }
    } catch (invalidoperationexception) {
        //file is in use
        return false;
    }

    return false;
}

score:1

just try this out .should work .haven't tested though

  var fw = from f in new directoryinfo("c:\\users\\user\\downloads\\").getfiles()
                where securitymanager.isgranted(new fileiopermission
 (fileiopermissionaccess.write, f.fullname))
                select f;

edit if it is just read only files then try this

var fe = from f in new directoryinfo("c:\\users\\ashley\\downloads\\").getfiles()
                where f.isreadonly==true
                select f

score:1

you run the risk of a race condition if you check for read permission prior to opening the file.

if you're attempting to read all of the files you have access to in a folder, better to just try opening each one and catch the unauthorizedaccessexception.

see:


Related Query

More Query from same tag