Directory.GetFiles方法在第一次遇到没有访问权限的文件夹时失败.
该方法抛出一个UnauthorizedAccessException(可以捕获),但到此时,该方法已经失败/终止.
我正在使用的代码如下:
try
{
// looks in stated directory and returns the path of all files found
getFiles = Directory.GetFiles(
@directoryToSearch,
filetype,
SearchOption.AllDirectories);
}
catch (UnauthorizedAccessException)
{
}
Run Code Online (Sandbox Code Playgroud)
据我所知,没有办法事先检查某个文件夹是否具有定义的访问权限.
在我的示例中,我正在通过网络搜索磁盘,当我遇到仅限root访问权限的文件夹时,我的程序失败了.
我在列表中有一组字符串路径,如["x1/x2/x3","x1/x2/x4","x1/x5"].我需要从这个列表中构造一个树状结构,可以迭代得到一个漂亮的打印树.像这样
x1
/ \
x5 x2
/ \
x3 x4
Run Code Online (Sandbox Code Playgroud)
有什么想法/建议吗?我相信通过处理字符串列表可以首先攻击问题EDIT:选择的正确答案是优雅的实现,其他建议也很好.