启动FileSystemWatcher时如何处理UnauthorizedAccessException

时间:2019-05-22 15:34:11

标签: c# filesystemwatcher unauthorizedaccessexcepti

为包含无法访问的文件夹的路径启动FileSystemWatcher时出现错误。 即使文件系统也无法访问该文件夹,如果我尝试访问该文件夹,则会收到一条弹出消息,要求我授予对用户的永久访问权限,这是完全可以的,但是我想在我的应用程序中对其进行处理以正确启动该文件夹。观察者。

_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;

我得到的例外是这个

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)

路径存在,它在一个我具有读/写权限的usb密钥内,它只是一个引发问题的特定文件夹。 有没有办法安全地捕获此异常并启动观察程序?

1 个答案:

答案 0 :(得分:0)

您可以先检查权限:

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;
}