本文介绍了在C#中递归搜索目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要递归遍历C#中的目录。我正在做类似的操作。但是在遍历系统文件夹时会引发异常。

I need to recursively traverse directories in C#. I'm doing something like this. But it throws exception when iterating through system folders. How to check it before exception is thrown?

推荐答案

您必须检查访问权限,但我还是希望抓住它相反,例外使代码更易于理解,并且还可以处理其他问题,例如当您想远程解析目录并且网络崩溃时...

You would have to check the access rights, but I would urge to to catch the exceptions instead, that makes the code easier to understand and would also deal with other issues like when you want to parse directories remotely and network gets down...

为目录树创建GUI,您还可以在其他地方获得访问权限异常的地方添加一些漂亮的Lock图标,在其他地方添加Error图标...在C#中已经有一个漂亮的来启动它。

If you make a GUI for a directory tree you could also add some nice Lock icons on the places where you get access right exceptions and Error icons elsewhere... In C# there is already a nice free open source component for starting that.

如果要计算文件和大小无法解决版权问题,您必须在特权更高的用户下运行工具。

If you want to count files and sizes there is no way to overcome the rights issue, you have to run your tool under a more privileged user.

这篇关于在C#中递归搜索目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:02