Frage

inside my code i am using this command:

conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source =C:\Users\ori\Documents\Visual Studio 2013\Projects\maxstar01\Database.accdb");

i am in a team of programmers which we all work on our own computers but everyone are using the same folder name "maxstar01" (which inside there is the solution file and all of it's directories and that database file).

so i want to use a relative path which will call the database.accdb that sits inside the maxstar01 (without all of the path before that).

thanks you!

War es hilfreich?

Lösung

You can use AppDomain.CurrentDomain.BaseDirectory to get the application directory.

conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database.accdb"));

Edit : When developing a Windows/Console app, AppDomain.CurrentDomain.BaseDirectory may point to the "\bin\debug" directory instead of the root. In that case, you could use ..\..\ to move two levels up and obtain your program's root directory.

conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\Database.accdb"));

If you'd like to write code that works with both Web and Windows applications, you could take a look at this answer.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top