0

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.

jason
  • 6,962
  • 36
  • 117
  • 198
  • Please elaborate your question - what does it mean _I don't know how to use this code in my project?_ What is your current code? What are you trying to achieve? – Paweł Hemperek Oct 11 '16 at 05:14
  • Assign it to a variable or pass it to a method like anything else? I believe the question needs some more clarification as to what you actually need help with. – tphx Oct 11 '16 at 05:16
  • @PawełHemperek I edited the question, thanks. – jason Oct 11 '16 at 05:29

2 Answers2

2

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 );
Community
  • 1
  • 1
D34th
  • 301
  • 4
  • 8
1
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));
Paweł Hemperek
  • 1,130
  • 10
  • 21