A Coder
A Coder

Reputation: 3056

Create a folder in C# project dynamically

I have to create a folder in C# project dynamically and add files to it.

Tried the below code , but it creates the folder inside the bin folder. I need to create in the project running path itself.

//Code:

// Specify a name for your top-level folder. 
string folderName = @"..\Top-Level Folder";

System.IO.Directory.CreateDirectory(folderName);

Where i'm wrong?

Upvotes: 2

Views: 9877

Answers (2)

Mzf
Mzf

Reputation: 5260

Not sure what you are trying to achieve but you can use the project properties to create pre/post build actions.

The reason it was created under bin folder is that the code is run from bin/debug

If you can say what you are trying to do maybe there is better solution for it

Upvotes: 0

Ehsan
Ehsan

Reputation: 32729

I guess you are looking for this

 string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
 string folderName = Path.Combine(projectPath, "Top-Level Folder");
 System.IO.Directory.CreateDirectory(folderName);

Upvotes: 7

Related Questions