I have a method that takes a path to an excel file and opens it.
Instead of typing out the entire file path (like "C:\Users\..."), I want to be able to type "test.csv" and have the program access the csv file in its current directory.
The reason is that if I wish to zip the project and send it to someone else, I want the other person to be able to run it and see the same result that I do without having to go somewhere in their C:\ drive and manually create the excel file.
I'm using the following code to check if the path exists:
if (System.IO.File.Exists(path))
However, I'm unable to figure out which directory is the default directory so that I can say 'method("test.csv")'.
Edit: I placed the excel file in the bin\Debug directory (which has a bunch of .dll and .exe files) but the method call still throws an error that the file is not found. I'm getting the following error: "COMException was unhandled - 'test.csv' could not be found." When I click View Detail, I see "System.Runtime.InteropServices.COMException".
Edit 2: Here is what I have in the method:
appExcel = new Microsoft.Office.Interop.Excel.Application();
if (System.IO.File.Exists(path))
{
aWorkbook = appExcel.Workbooks.Open(path, true, true);
aWorkSheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
}
else
{
Console.WriteLine("Unable to open file");
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
appExcel = null;
}
The program is stopping at the line, 'aWorkbook = appExcel.Workbooks.Open(path, true, true)' with the unhandled COMException.