2014-01-08 167 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);