Search code examples
.netvb.netwinforms.net-4.0oledb

Relative path is returning unexpected value


This line of code

Assembly.LoadFrom(".\MyAssembly.dll")

Is throwing the following error on one machine:

System.IO.FileNotFoundException: Could not load file or assembly 'file:///\\Server1\Data\MyAssembly.dll' or one of its dependencies. The system cannot find the file specified.

MyAssembly.dll is present in the install location of the application (which is not on Server1) and all other instances of the application find the assembly correctly. I am not calling

Directory.SetCurrentDirectory

anywhere in the application and the application does not use any PATH or other environment variables.

The problem started just after the application threw a different error:

System.Data.OleDb.OleDbException: Undefined function 'todate' in expression.

based on the following connection string

Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\Server1\Data\MyDataBase.accdb;Persist Security Info=False;

Is the call to OLEDB setting the current directory and then leaving it as \\Server1\Data because it failed?

Is there some environment variable I should look for on this machine that could be setting the current directory for my application?


Solution

  • Don't assume the current directory is constant.

    The user can create a short-cut with a different working directory, or can launch your app from an arbitrary directory on command prompt.
    And also, some particular methods of the standard library can change the current directory at runtime.
    (I know OpenFileDialog/SaveFileDialog (without setting RestoreDirectory true) do that.)

    OLEDB might also change the current directory, but don't care.
    Never depend on the current directory.

    To get your app's directory, use System.Windows.Forms.Application.StartupPath instead.