Reputation: 103
I have tried filesystemwatcher - but it is no use to monitor access to file. Is there any interface to do so?
Upvotes: 0
Views: 821
Reputation: 46040
For simple auditing you can use audit policy. If you want to take action before actual access happens (or prevent access based on some complicated logic)m then you need to employ a filesystem filter driver. Write your own (kernel-mode driver writing skills and several months of work required) or use our CallbackFilter product, which provides a driver and lets you write business logic in user-mode including .NET.
Upvotes: 0
Reputation:
Catch UnauthorizedAccessException
,if caught then You have no access.
Upvotes: 1
Reputation: 7082
You can read more about FileIOPermission
class.
or use custom method like this:
public static bool HasWritePermissionOnDir(string path)
{
var writeAllow = false;
var writeDeny = false;
var accessControlList = Directory.GetAccessControl(path);
if(accessControlList == null)
return false;
var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
if(accessRules ==null)
return false;
foreach (FileSystemAccessRule rule in accessRules)
{
if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write)
continue;
if (rule.AccessControlType == AccessControlType.Allow)
writeAllow = true;
else if (rule.AccessControlType == AccessControlType.Deny)
writeDeny = true;
}
return writeAllow && !writeDeny;
}
Upvotes: 0