Checking if a file is readable

K

K Viltersten

I'd like to know if a file, a path to which i provide,
is readable with respect to the access rights for
the currently logged-in user.

I've got stuck. After some heavy googling i've
come to the following but no further.

FileInfo file = new FileInfo( path );
FileSecurity fileSecurity = file.GetAccessControl();

I guess i need to connect it somehow with the
FileSystemRights.Read value but i simply don't see
how it should be done. Suggestions?
 
A

Alberto Poblacion

K Viltersten said:
I'd like to know if a file, a path to which i provide,
is readable with respect to the access rights for
the currently logged-in user.

I've got stuck. After some heavy googling i've
come to the following but no further.

FileInfo file = new FileInfo( path );
FileSecurity fileSecurity = file.GetAccessControl();

I guess i need to connect it somehow with the
FileSystemRights.Read value but i simply don't see
how it should be done. Suggestions?

This is going to be quite complex. You would have to enumerate all the
access rules in the FileSecurity, and see which rules apply to the current
user. The user could be a member of several groups, some of which could
grant permissions on the file, or even a group that *denies* permission,
which would take precedence over the grants.

I suggest that (instead) you try to read the file inside a try...catch
block.
 
K

K Viltersten

I'd like to know if a file, a path to which i provide,
This is going to be quite complex. You would have to enumerate all the
access rules in the FileSecurity, and see which rules apply to the current
user. The user could be a member of several groups, some of which could
grant permissions on the file, or even a group that *denies* permission,
which would take precedence over the grants.

I suggest that (instead) you try to read the file inside a try...catch
block.

Thanks. That's what i suspected.
 
J

Jeroen Mostert

K said:
I'd like to know if a file, a path to which i provide,
is readable with respect to the access rights for
the currently logged-in user.

I've got stuck. After some heavy googling i've
come to the following but no further.

FileInfo file = new FileInfo( path );
FileSecurity fileSecurity = file.GetAccessControl();

I guess i need to connect it somehow with the
FileSystemRights.Read value but i simply don't see
how it should be done. Suggestions?
As Alberto suggested, simply trying to read the file and seeing if it fails
is much simpler and exactly what's wanted in most cases. If you need to read
the file, just do so, don't check first. Things might change between the
time you check and the time you read anyway.

That said, if you really must know whether someone has permission to read
the file without accessing its contents, probably the simplest method of
finding out is through WMI:

using (
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
@"\\.\root\cimv2", @"SELECT * FROM CIM_DataFile WHERE Name =
'C:\\boot.ini'" // mind the double escaping
)
) {
bool canRead = false;
foreach (ManagementObject mo in searcher.Get()) {
canRead = Convert.ToBoolean(mo.InvokeMethod("GetEffectivePermission",
new object[] { 1 })); // FILE_READ
}
}

There's quite a lot of overhead associated with this, though, so decide for
yourself if it's worth the cost.
 
A

Arne Vajhøj

K said:
Thanks. That's what i suspected.

You would need the try catch anyway - just in case protection
was changed between your check and the actual reading.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top