搜索文件夹中的文件而不会获得UnauthorizedAccessException [重复](Search folders for files without getting Unauthorized

编程入门 行业动态 更新时间:2024-10-18 16:43:56
搜索文件夹中的文件而不会获得UnauthorizedAccessException [重复](Search folders for files without getting UnauthorizedAccessException [duplicate])

这个问题在这里已有答案:

当Directory.GetFiles()被拒绝访问 8个答案 时忽略文件夹/文件

我一直在尝试在“我的文档”中的所有可能目录中查找文件,但由于“我的音乐”文件夹,它会抛出“System.UnauthorizedAccessException”异常。

我想可能列出“我的文档”中的所有文件夹,然后获取“我的音乐”的路径并将其排除在搜索之外将解决问题,但它失败了......

所以现在我不知道如何在“我的文档”中的所有文件夹中找到特定的“.ini”文件而不会出现异常,有人可以帮我吗? :)

我用过的代码:

DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); foreach(FileInfo file in dir.GetFiles("*.ini", SearchOption.AllDirectories)) { listBox1.Items.Add(file.Name); }

This question already has an answer here:

Ignore folders/files when Directory.GetFiles() is denied access 8 answers

I have been trying to look for a file in all the possible directories inside "My Documents", but it's throwing the "System.UnauthorizedAccessException" exception because of the "My music" folder.

I thought maybe listing all the folders within "My documents", then getting the path of "My music" and excluding it from being searched would solve the problem, but it failed...

So now I have no idea how to find a specific ".ini" file within all the folders inside "My documents" without getting that exception, can anyone help me please? :)

The code I used:

DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); foreach(FileInfo file in dir.GetFiles("*.ini", SearchOption.AllDirectories)) { listBox1.Items.Add(file.Name); }

最满意答案

除了当前的System.UnauthorizedAccessException之外,还有许多问题需要处理,例如“重新分析点”(硬链接,交接点,符号链接等)。

在进一步讨论之前,你真的应该阅读这篇文章:

文件夹递归

它没有涵盖我脑海中浮现的所有问题,但文章中给出的解决方案是朝着正确方向迈出的非常好的一步,可以避免许多“未知”。

编辑://

我提到重新分析点,因为在Windows 7,8中使用它们(AFAIK)等构成了SpecialFolder对象。

例如:

"C:\Users\UserName\My Documents"是一个NTFS联结,指向"C:\Users\UserName\Documents" 。

我认为重要的是指出这一点,因为没有明确的规则会阻止我在"C:\Users\UserName\My Documents"上设置ACL以禁止任何人,但管理员不能访问"C:\Users\UserName\Documents"通过交汇点/符号链接,从而发送给你另一个兔子洞:-)

There are many issues to deal with other than your current System.UnauthorizedAccessException such as "reparse points" (hard links, junction points, symbolic links, etc).

You really should read this article before going ANY further:

Folder Recursion

It doesn't cover all of the issues that come to my mind, but the solution given in the article is a very good step in the right direction to avoid a lot of 'unknowns'.

Edit://

I mention reparse points because these are used (AFAIK) in Windows 7, 8?, etc to constitute the SpecialFolder objects.

For example:

"C:\Users\UserName\My Documents" is a NTFS junction which points to "C:\Users\UserName\Documents".

I feel it is important to point this out because there is no explicit rule which would prevent me from setting the ACL on "C:\Users\UserName\My Documents" to disallow anyone but, say, Administrators from accessing "C:\Users\UserName\Documents" via the junction/ symbolic link, thus sending you down yet another rabbit hole :-)

更多推荐

本文发布于:2023-08-04 10:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   夹中   UnauthorizedAccessException   Search   files

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!