2014-03-01 100 views

回答

3

您可以使用重載的方法Directory.GetFiles(string,string) topass是searchPattern爲*.dll獲得與.dll

Directory.GetFiles()

擴展的所有文件,則返回匹配指定 的文件(包括其路徑)的名字在指定的目錄中搜索模式。

試試這個:

Directory.GetFiles(DIRname,"*.dll") 
+0

KK對不起我遲鈍。 – Andrew

+0

@Andrew:如果這個答案解決了你的問題,你應該將其標記爲已接受。 –

+0

@JimMischel,你剛纔忘記了(由於時間限制)。 – Andrew

0

File.Delete不支持任何通配符。

您可以使用以下兩個選項之一(使用「* .dll」通配符,您可以根據需要更改它)。

獲取所有文件並使用Parallel.ForEach並行刪除它們,比普通循環快得多。

var files = Directory.GetFiles(@"c:\mypath", "*.dll"); 
    Parallel.ForEach(files, (f)=> File.Delete(f)); 

或者,使用Windows命令外殼用DEL命令:

Process process = new Process() 
    { 
     StartInfo = new ProcessStartInfo() 
     { 
      UseShellExecute = false, 
      RedirectStandardOutput = true, 
      CreateNoWindow = true, 
      RedirectStandardInput = true, 
      RedirectStandardError = true, 
      FileName = "cmd.exe", 
      Arguments = @"/C del c:\mypath\*.dll", 

     } 
    }; 
    process.Start(); 
    process.WaitForExit(); 
+0

並行刪除不太可能會更快。文件刪除的限制因素是文件系統訪問,這將被序列化。 –

+0

確實如此,但固態硬盤可能會帶來一些優勢。可能會運行一個基準這是一個好主意:) – loopedcode

+1

再一次,極不可能。訪問目錄信息是序列化的,因此它是否旋轉鏽蝕或SSD或RAM磁盤無關緊要。系統每個邏輯驅動器一次只能刪除一個文件。 –

相關問題