jason
jason

Reputation: 7164

Getting a directory path using project directory

I have a project whose name is Myproject.Application.DataExporter. I'm saving a file to a directory whose name is Export. I'm using this code :

workbook.Save(@"D:\workspace\MyApp\Myproject.Application.DataExporter\Export\Calc.xlsx");

I see how to get current project directory in Stackoverflow. There is this code :

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;

I want to save file under Export directory, using a generic path like the code above. It gives current project directory. I want to use it for adding a file to Export directory. How can I use it in my project for adding a file to Export? Thanks.

Upvotes: 0

Views: 9767

Answers (2)

D34th
D34th

Reputation: 331

As per the other previous comments you need to clarify your question. I'm assuming you want to get the current directory and then save a file to it so the path is relative to where you host your application?

You could just do this:

workbook.Save(Path.Combine(Environment.CurrentDirectory, "Calc.xlsx");

To get path relative to the your current working directory see here: Getting path relative to the current working directory?

EDIT: Can't comment as not enough reputation. When you do a Directory.GetCurrentDirectory() what is the value that is returned?

If Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName returns the "Export" folder path you could just do this:

string fileName = "Calc.xlsx";        
workbook.Save(Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, fileName );

Upvotes: 2

Paweł Hemperek
Paweł Hemperek

Reputation: 1180

string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string path = Path.Combine(dir, "Myproject.Application.DataExporter", "Export");
string file = "Calc.xlsx";
workbook.Save(Path.Combine(path, file));

or if you want to skip Myproject.Application.DataExporter

string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string path = Path.Combine(dir, "Export");
string file = "Calc.xlsx";
workbook.Save(Path.Combine(path, file));

Upvotes: 1

Related Questions