2016-12-27 58 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

回答