1

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.

covfefe
  • 2,485
  • 8
  • 47
  • 77
  • The initial [Working Directory](http://en.wikipedia.org/wiki/Working_directory) is "where" you are in the shell when launching the executable.. or as specified in the LNK file, etc. As long as the CSV file is *relative* to the correct WD then it will just work without specifying an absolute path. – user2864740 Jun 26 '14 at 21:03
  • You don't have to zip the file and send it to someone else to test this. Try something, copy your deploy directory to somewhere else on disk and then run the program. Anyway just add it to your project as content and set "Copy On Build" to another option than "Never". – CodeCaster Jun 26 '14 at 21:03

5 Answers5

1

To get the current directory path you can use Directory.GetGetCurrentDirectory().

Additionally, you can find the location of your assembly via Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), and then use that path as your base path.

For example for the file.csv located right beside your assemblies you can use the following:

string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string filePath = Path.Combine(basePath, "file.csv");
Vadym Stetsiak
  • 1,974
  • 18
  • 22
  • I put this code in and printed out the strings 'basepath' and 'filepath' and they were in the same directory. So the file seems to be in the correct directory, but I'm still getting an unhandled COMException. – covfefe Jun 27 '14 at 14:32
0

Place the file in the bin directory - i.e. in the same directory as your dlls or exe. That way you don't have to specify a path. Warning, things get messy though. Make sure you think about clean up too.

Zuzlx
  • 1,246
  • 14
  • 33
0

Have a look at the Application object - It gives you plenty of path options... Based upon what I believe you're asking check out StartupPath.

Hope this does the trick!

John Bustos
  • 19,036
  • 17
  • 89
  • 151
0

The default should be the execution path. I believe it is in /bin/Debug or /bin/Release, depending on your build. That is where the .exe or .dll files will be written.

pmaj
  • 51
  • 7
0

The directory that is being used by the Excel object is different from the directory that C# uses. So in this line

appExcel = new Microsoft.Office.Interop.Excel.Application();

The default directory can be found if you look into its properties during Debug mode. There is a way to change this default directory using a macro, but it would probably be easier to add in the following line in the if-statement:

path = System.IO.Path.GetFullPath(path);

This way, you can keep the Excel file in the C# default directory, and automatically find the complete filepath to it.

More details on using the macro and this topic in general can be found here.

Community
  • 1
  • 1
covfefe
  • 2,485
  • 8
  • 47
  • 77