UnauthorizedAccessException,同时扫描目录'User \Documents\My Music' [英] UnauthorizedAccessException while scanning directory 'User\Documents\My Music'

查看:172
本文介绍了UnauthorizedAccessException,同时扫描目录'User \Documents\My Music'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 问题:为什么在扫描用户的我的文档文件夹时出现此错误, 次要 重要性较低的问题:有没有办法避免这种情况,而不必专门过滤这些文件夹或使用try / catch块?

  1. Question: Why do I get this error while scanning a users 'My Documents' folder, but not when I scan the 'My Music/My Pictures/My Videos' directory?
  2. Secondary, less important question: Is there a way to avoid this without having to specifically filter these folders out, or using a try/catch block?

喜欢教我如何钓鱼的答案,而不是只给我鱼。就在这一点上,我不知道我需要查找具体回答这个问题。我已阅读关于提升权限遍历文件系统,花了一个好星期寻找为什么我可以设置DirectoryInfo'User\我的音乐,但不是User \Documents \My Music(链接),只是在一个不同的方向,在学习更多的方面有所增加。

I prefer answers that teach me how to fish, instead of just giving me fish. Just at this point I am not sure where I need to look to specifically answer this question. I've read through documents about elevating permissions and iterating through the file system, and spent a good week looking for why I can set DirectoryInfo on 'User\My Music' but not 'User\Documents\My Music'(link) and just would enjoy a little boost in a different direction in regards to learning more.

我捕获最初引发的初始UnauthorizedAccessException,当尝试Directory.GetFiles('path',*,SearchOption.AllDirectories)其中path是用户的我的文档。要处理异常,我知道我需要手动走目录。

I catch the initial 'UnauthorizedAccessException' that is thrown initially when attempting Directory.GetFiles('path', "*", SearchOption.AllDirectories) where path is the users 'My Documents'. To handle the exception I know that I need to walk the directory manually. Which works, returning the files from the sub-directories.

初始GetFiles函数的代码:

The code for the initial GetFiles function:

public static string[] GetFiles(string path)
{
    string[] files;
    try
    {
        files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
    }
    catch(UnauthorizedAccessException ex)
    { files = WalkDirectory(path); }
    return files;
}

public static string[] WalkDirectory(string path)
{
    List<string> files = new List<string>();
    DirectoryInfo dir = new DirectoryInfo(path);

    foreach (DirectoryInfo subDir in dir.GetDirectories())
    {
        try
        {
            files.AddRange(WalkDirectory(subDir.FullName));
        }
        catch(UnauthorizedAccessException ex)
        {
            // complete fail to walk directory listed
            throw ex;
        }
    }
    foreach (FileInfo file in dir.GetFiles())
    {
        files.Add(file.FullName);
    }
}

这完全可行,直到代码尝试走隐藏的文件夹:我的音乐,我的图片或我的视频。无论我如何尝试和重新编码来走隐藏的文件,我一直收到UnauthorizedAccessException。

This works out perfectly, until the code attempts to walk the hidden folders: My Music, My Pictures, or My Videos. No matter how I try and re-code to walk the hidden files, I keep receiving the UnauthorizedAccessException.

我完全理解,我要解决这个问题。主要是我很好奇地知道,是为什么异常发生在用户文件夹下?

I understand completely that I am going to code around this. Mainly what I am curious to know, is why is the exception happening under a users folder?

我所做的一个假设是,该文件夹是到另一个目录的符号链接,因为我可以使路径?:\users directory\user\\ \\ My(音乐,图片或视频),然后代码走过那些目录,没有任何问题。这只会在尝试在用户我的文档中设置后扫描目录文件时才会发生。

An asssumption I am making is that the folder is a symlink to another directory, because I can make the path ?:\users directory\user\My (Music, Pictures, or Videos) and the code walks those directories then without any issues. This only happens when trying to scan the directory files after setting them from within the users My Documents.


  • 操作系统: Windows 7

  • 用户权限:管理员

  • / li>
  • OS: Windows 7
  • User Privliages: Administrator
  • Application Elevated to run as administrator

推荐答案

我和一个朋友谈论这个问题,举行对话,他帮助我进一步缩小这个问题。这实际上是一个重复的问题,并在检查是否一个文件是真实的或符号链接

I was speaking about this with a friend, who is not technical, but knows enough tech to hold a conversation and he helped me narrow this question down further. This is actually a duplicate question and was answered at Check if a file is real or a symbolic link.

文件夹是一个符号链接,根据 TechRepublic:

The folder is a symbolic link that was placed there for backwards compatibility purposes according to this article on TechRepublic: Answers to some common questions about symbolic links under the section Windows Vista and Windows 7 have built-in symbolic links paragraph 2.

为了特别避免在UnauthorizedAccessException上尝试扫描此目录而没有Try / Catch块,需要检查文件夹属性以确定所讨论的文件夹或文件是否是符号链接中。这也是在上面列出的stackoverflow问题中回答。

In order to specifically avoid attempting to scan this directory without a Try/Catch block on an UnauthorizedAccessException the folder attributes need to be checked to determine if the folder or file in question is a symbolic link. Which again was answered in the above listed stackoverflow question.

这篇关于UnauthorizedAccessException,同时扫描目录'User \Documents\My Music'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆