Re: How check is app has file write security permissions?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Laurent,

Thank you for your reply.

Try to write, and catch the Exception.

Good idea, that would have been much easier. I ended up writing a lot of code using the following method:


/// <summary>
/// Returns true if specified identity name has the file system
/// rights for the specified file
/// </summary>
private static bool IdentityHasAccces(string identityName,
FileInfo fileInfo,
FileSystemRights
fileSystemRights)
{
identityName = identityName.ToUpper();
AuthorizationRuleCollection authorizationRuleCollection =
fileInfo.GetAccessControl().GetAccessRules(true, true,typeof(NTAccount));

foreach (FileSystemAccessRule fileSystemAccessRule in authorizationRuleCollection)
{
if (identityName == fileSystemAccessRule.IdentityReference.Value.ToUpper())
{
return AccessControlType.Allow == fileSystemAccessRule.AccessControlType && fileSystemRights ==(fileSystemAccessRule.FileSystemRights & fileSystemRights);
}
}
return false;
}

Example Usage:
--------------

if (false == IdentityHasAccces(System.Security.Principal.WindowsIdentity.GetCurrent().Name,
dbFileInfo,
FileSystemRights.Read | FileSystemRights.Write))
{
return;
}

.