2014-01-08 156 views
0

例如,如果我想要獲取項目目錄中的文件數量。 在Java我會寫:如何獲取項目目錄的路徑

int numberOfFiles = new File(".").listFiles().length; 

但我不知道怎麼去路徑項目在.NET目錄。如何在C#中實現相同的目標?

+0

可能重複:http://stackoverflow.com/questions/1658518/getting-the-absolute-path-of-the-executable- using-c – Pedrom

回答

2

它是簡單

var path = Path.GetDirectoryName(Application.ExecutablePath); 
var files = Directory.GetFiles(path); 
1

它很接近 - 使用:

int numberOfFiles = Directory.GetFiles(".").Length 
+0

與John Koerner解決方案相比,是否有任何區別(結果,效果)? – Yoda

+1

_result_應該是相同的。您必須嘗試兩種方法來查看_performance_是否不同。 –

+0

你更喜歡哪一個?我想知道參考解決方案 - >純粹的解決方案。 – Yoda

1

可以使用Application.ExecutablePath獲取可執行文件的路徑,然後從那裏得到的目錄。一旦你有了目錄,很容易得到的文件數:

var executingDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath); 
var numFiles = System.IO.Directory.GetFiles(executingDir).Count(); 
Console.WriteLine(numFiles);