2016-12-27 55 views
0

我想在我的系统上显示所有.txt文件。但是当我执行我的代码时,我只能在我的可移动磁盘驱动器(USB-Stick)上显示.txt文件。C#无法搜索c:驱动器上的.txt文件

我该如何解决这个问题?

谢谢您的帮助:d

这里是我的代码:

static List<string> Search() 
{ 
    var files = new List<string>(); 

    foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady)) 
    { 
     try 
     { 
      files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "*.txt", SearchOption.AllDirectories)); 
      Console.Write(string.Join(System.Environment.NewLine, files)); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
     } 
    } 

    return files; 
} 

我的控制台输出:

The access to the path "C: \$ Recycling. Bin\S-1-5-18" was refused. 
D:\Test\Test1.txt 
D:\Test\Test2.txt 
... 
D:\PortableApps\EclipsePortable\Other\Source\Readme.txtSystem.Collections.Generic.List`1[System.String] 

我完全不收到任何错误讯息。

+0

您是否遇到某种错误?如果是这样,请发布它的详细信息。 –

+0

对不起,我应该发布我的控制台输出^^编辑我的帖子 – itsPauV

+0

我得到同样的错误。以admin身份运行,但在c:\ documents和settings中出现错误。最好跳过那些目录。 – bwoogie

回答