【发布时间】:2016-01-04 02:05:09
【问题描述】:
我是 C# 新手。我有一个文本框,我在其中输入要搜索的文件和一个“搜索”按钮。在搜索时钟上,我希望它填充文件夹中的文件,但出现上述错误。以下是我的代码:
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
}
}
请帮我解决它。谢谢
【问题讨论】:
-
很明显你不应该进入那个垃圾箱。
-
只是对 d: 上的所有目录进行全面搜索会导致 大量 系统文件夹普通用户帐户不应访问,并且您将被拒绝访问。
-
我的 D 盘没有回收站。我只是在搜索文件夹或文件。我知道我们不应该扔垃圾。请让我知道我是如何避免在垃圾箱中搜索的。
-
在 d: 盘上确实有一个回收站,即使它隐藏在文件资源管理器中。除此之外,使用
AllDirectories时不能只跳过文件夹,最好是catchUnauthorizedAccessException并跳过该目录。
标签: c#