I'm working on a project where I have to perform sanity checks on files. I need to make sure the current system user has read access to the file, and I first tried doing it with:
var readPermission = new FileIOPermission(FileIOPermissionAccess.Read, filePath);
try
{
readPermission.Demand();
}
catch (SecurityException ex)
{
//handle the exception, which should be thrown if current user does NOT have the read permission
}
That did not work, e.g. no exception was thrown, and so i tried doing this:
var readPermission = new FileIOPermission(FileIOPermissionAccess.Read, filePath);
if(! SecurityManager.IsGranted(readPermission))
{
throw new SecurityException(
String.Format("System user: {0} does not have read access to file {1}", User.Identity.Name, filePath)
);
}
the SecurityManager api seems to be mostly deprecated however, So that also seems like a dead end. Is there some other way to tell what permissions a user has to a file?