解决方案或 bin/debug 文件夹中 Excel 文件的 C# 文件路径?

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

我试图访问一个 Excel 文件,但没有在我的电脑上指定整个绝对路径。

取而代之的是:

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);

我想始终动态地使用解决方案中提供的模板。

有什么提示或想法吗?

好的,如果您想从应用程序的目录中获取文件,请使用这个,

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