Search code examples
c#pathopenfiledialog

C# filepath to Excel file in solution or bin/debug folder?


I am trying to reach an Excel file withouth specifying the whole absolute path on my pc.

In stead of this:

Excel.Application app = new Excel.Application();
OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            Excel.Workbook sheet = app.Workbooks.Open(ofd.FileName);
           //etc
        }
SaveFileDialog sfd = new SaveFileDialog();
sfd.ShowDialog();
string name = sfd.FileName;
sheet.SaveAs(name);

I would like to always use the template provided in the solution, dynamically.

Any tips or ideas?


Solution

  • Ok, if you want to get a file from your application's directory use this,

     Excel.Workbook sheet = app.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "ExcelFile.xlsx");