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

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

问题描述


  1. 问题:为什么在扫描用户的我的文档文件夹时收到此错误,但是当我扫描我的音乐/我的图片/我的视频目录?

  2. 次要, 不太重要的问题有一种方法来避免这种情况,而无需专门过滤这些文件夹,或使用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?

我喜欢答案教我如何钓鱼,而不是只给我鱼。只是在这一点上,我不知道我需要特别回答这个问题。我已经阅读了关于升级权限遍历文件系统,并花了好一周的时间寻找为什么我可以在User\中设置DirectoryInfo我的音乐,但不是用户\文档\\我的音乐(链接),只是在学习更多方面在不同方向享受一点提升。

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.

我尝试在尝试Directory.GetFiles('path',*,SearchOption.AllDirectories)的路径是用户的我的文档时最初抛出的初始UnauthorizedAccessException。要处理异常,我知道我需要手动走路。哪些工作,从子目录返回文件。

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

  • 用户权限管理员

  • 应用程序升级到以管理员身份运行

  • 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:关于符号链接的一些常见问题的解答 Windows Vista和Windows 7部分有内置的符号链接第2段。

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.

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

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