I'm getting an error when starting a FileSystemWatcher for a path that contains an inaccessible folder. The folder is not accessible even by file system, if I try to access it I get a popup message asking me to grant permanent access to my user, this is completely fine but I'd like to handle it in my application to correctly start the watcher.
_watcher = new FileSystemWatcher(_path, _filter)
{
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName
};
_watcher.Changed += new FileSystemEventHandler(OnChanged);
_watcher.Created += new FileSystemEventHandler(OnCreated);
_watcher.Deleted += new FileSystemEventHandler(OnDeleted);
_watcher.Renamed += new RenamedEventHandler(OnRenamed);
_watcher.EnableRaisingEvents = true;
The exception I'm getting is this
UnauthorizedAccessException: Access to the path 'D:\some_folder' is denied.
System.IO.__Error.WinIOError (System.Int32 errorCode, System.String maybeFullPath) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.FileSystemEnumerableIterator`1[TSource].HandleError (System.Int32 hr, System.String path) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.FileSystemEnumerableIterator`1[TSource].CommonInit () (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.FileSystemEnumerableIterator`1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler`1[TSource] resultHandler, System.Boolean checkHost) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.FileSystemEnumerableFactory.CreateFileNameIterator (System.String path, System.String originalUserPath, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.Directory.InternalGetFileDirectoryNames (System.String path, System.String userPathOriginal, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.Directory.InternalGetDirectories (System.String path, System.String searchPattern, System.IO.SearchOption searchOption) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.Directory.GetDirectories (System.String path) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
System.IO.DefaultWatcher.DoFiles (System.IO.DefaultWatcherData data, System.String directory, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.DoFiles (System.IO.DefaultWatcherData data, System.String directory, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.DoFiles (System.IO.DefaultWatcherData data, System.String directory, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.DoFiles (System.IO.DefaultWatcherData data, System.String directory, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.DoFiles (System.IO.DefaultWatcherData data, System.String directory, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.DoFiles (System.IO.DefaultWatcherData data, System.String directory, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.DoFiles (System.IO.DefaultWatcherData data, System.String directory, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.UpdateDataAndDispatch (System.IO.DefaultWatcherData data, System.Boolean dispatch) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.DefaultWatcher.StartDispatching (System.IO.FileSystemWatcher fsw) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.FileSystemWatcher.Start () (at <4b9f316768174388be8ae5baf2e6cc02>:0)
System.IO.FileSystemWatcher.set_EnableRaisingEvents (System.Boolean value) (at <4b9f316768174388be8ae5baf2e6cc02>:0)
(wrapper remoting-invoke-with-check) System.IO.FileSystemWatcher.set_EnableRaisingEvents(bool)
The paths exists, it's inside an usb key for which I have read/write permissions, it's only a specific folder which raises the issue. Is there a way to safely catch this exception and start the watcher?
You could first check the permissions:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.