0

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!

Ori
  • 1,680
  • 21
  • 24

1 Answers1

3

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.

Community
  • 1
  • 1
JW Lim
  • 1,794
  • 3
  • 21
  • 41
  • It does not recognize "Path", any suggestions? – Ori Mar 18 '14 at 19:23
  • @user3431904 Include `using System.IO;` at the top of your code. – JW Lim Mar 19 '14 at 00:40
  • it goes to "...\MainProg\bin\debug\" , is there any way i can get to "MainProg" ? – Ori Mar 23 '14 at 12:21
  • @user3431904 I've updated my answer for your needs, have a look :) – JW Lim Mar 24 '14 at 02:12
  • 1
    You probably want it to point to the \debug folder anyway. This is the folder that will ultimately get deployed on a real install, while the project root lives only in development. The DB itself may be copied with the `Copy Always` or `Copy if newer` deploy option. – Alejandro Mar 24 '14 at 05:00
  • @user3431904 Has your problem been solved, or do you still require help with this matter? – JW Lim Apr 28 '14 at 02:05