Yoda
Yoda

Reputation: 18068

How to get path to the projects directory

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

Answers (3)

Selman Genç
Selman Genç

Reputation: 101732

It is simple

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

Upvotes: 2

John Koerner
John Koerner

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

D Stanley
D Stanley

Reputation: 152644

It's close - use:

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

Upvotes: 1

Related Questions