拒绝访问路径'd:\ $ RECYCLE.BIN \ S

编程入门 行业动态 更新时间:2024-10-28 08:16:44
本文介绍了拒绝访问路径'd:\ $ RECYCLE.BIN \ S-1-5-21-494745725-312220573-749543506-41600'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是C#的新手.我有一个文本框,在其中输入要搜索的文件和一个搜索"按钮.在搜索时钟上,我希望它填充文件夹中的文件,但出现上述错误.下面是我的代码:

I am new to C# . I have a text box where i enter the file to search and a 'search' button. on clock of search i want it to populate the files in the folder but i get the above error. Below is my code:

string[] directories = Directory.GetDirectories(@"d:\", "*", SearchOption.AllDirectories); string file = textBox1.Text; DataGrid dg = new DataGrid(); { var files = new List<string>(); foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady)) { try { files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, file , SearchOption.AllDirectories)); } catch(Exception ex) { MessageBox.Show("the exception is " + ex.ToString()); //Logger.Log(e.Message); // Log it and move on } }

请帮助我解决该问题.谢谢

Please help me resolve it . Thanks

推荐答案

在可能包含不可访问子文件夹的文件夹中进行搜索时,最重要的规则是:

The most important rule when searching on a folder which potentially contains inaccessible subfolder is:

不要不要使用 SearchOption.AllDirectories !

改为使用 SearchOption.TopDirectoryOnly ,结合递归搜索来访问所有可访问目录.

Use SearchOption.TopDirectoryOnly instead, combined with recursive search for all the accessible directories.

使用 SearchOption.AllDirectories ,一个访问冲突将中断整个循环,甚至在处理任何文件/目录之前.但是,如果您使用 SearchOption.TopDirectoryOnly ,则只会跳过无法访问的内容.

Using SearchOption.AllDirectories, one access violation will break your entire loop even before any file/directory is processed. But if you use SearchOption.TopDirectoryOnly, you only skip what is inaccessible.

还有一个更困难的方法来对每个子目录使用 Directory.GetAccessControl(),以检查您是否可以事先访问目录(尽管此选项相当困难-我不知道除非您确切知道访问系统的工作原理,否则不建议这样做.

There is more difficult way to use Directory.GetAccessControl() per child directory check to see if you have an access to a Directory before hand (this option is rather hard though - I don't really recommend this unless you know exactly how the access system works).

对于递归搜索,我实现了以下代码供自己使用:

For recursive search, I have this code implemented for my own use:

public static List<string> GetAllAccessibleDirectories(string path, string searchPattern) { List<string> dirPathList = new List<string>(); try { List<string> childDirPathList = Directory.GetDirectories(path, searchPattern, SearchOption.TopDirectoryOnly).ToList(); //use TopDirectoryOnly if (childDirPathList == null || childDirPathList.Count <= 0) //this directory has no child return null; foreach (string childDirPath in childDirPathList) { //foreach child directory, do recursive search dirPathList.Add(childDirPath); //add the path List<string> grandChildDirPath = GetAllAccessibleDirectories(childDirPath, searchPattern); if (grandChildDirPath != null && grandChildDirPath.Count > 0) //this child directory has children and nothing has gone wrong dirPathList.AddRange(grandChildDirPath.ToArray()); //add the grandchildren to the list } return dirPathList; //return the whole list found at this level } catch { return null; //something has gone wrong, return null } }

这就是你的称呼方式

List<string> accessibleDirs = GetAllAccessibleDirectories(myrootpath, "*");

然后,您只需要在所有可访问目录中搜索/添加文件.

Then, you only need to search/add the files among all accessible directories.

注意:这个问题虽然很经典.我相信还有其他更好的解决方案.

Note: this question is quite classical though. I believe there are some other better solutions out there too.

如果在获得所有可访问目录后,您特别希望避免某些目录,则还可以使用部分目录名作为关键字通过LINQ过滤 List 结果(即 Recycle.Bins ).

And in case there are some directories which you particularly want to avoid after you get all your accessible directories, you could also filter the List result by LINQ using part of the directory's name as keyword (i.e. Recycle.Bins).

更多推荐

拒绝访问路径'd:\ $ RECYCLE.BIN \ S

本文发布于:2023-11-08 02:26:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   BIN   RECYCLE

发布评论

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

>www.elefans.com

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