Reputation: 18068
For example if I would like to get number of files in the project's directory.
In Java
I would write:
int numberOfFiles = new File(".").listFiles().length;
But I do not know how to get path to the project's directory in .NET. How to achieve the same goal in C#?
Upvotes: 0
Views: 108
Reputation: 101732
It is simple
var path = Path.GetDirectoryName(Application.ExecutablePath);
var files = Directory.GetFiles(path);
Upvotes: 2
Reputation: 38087
You can get the path to the executable using Application.ExecutablePath
and then get the directory from there. Once you have the directory, it is easy to get the file count:
var executingDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
var numFiles = System.IO.Directory.GetFiles(executingDir).Count();
Console.WriteLine(numFiles);
Upvotes: 1
Reputation: 152644
It's close - use:
int numberOfFiles = Directory.GetFiles(".").Length
Upvotes: 1