我是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
发布评论