3

The connection string setting is below:

Name: dbPersonConnectionString

Type: Connection string Scope: Application

Value: Data Source=|DataDirectory|\dbPerson.sdf

When I install & run the application, it looks for DB in C:\MyApp\Data\ folder. It should be C:\MyApp without additional \Data folder.

Should I simply create Data folder in my project and move DB files under that folder or I simply adjust |DataDirectory| -and how-?

EDIT:

        string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
        string path = (System.IO.Path.GetDirectoryName(executable));
        AppDomain.CurrentDomain.SetData("DataDirectory",path);
Nime Cloud
  • 6,162
  • 14
  • 43
  • 75

3 Answers3

6

This has been asked before. This MSDN post gives a good overview.

It should indeed default to your binaries folder, you can change it with AppDomain.SetData() . If you change it, better do it early.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    An SO answer: http://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented/1409378#1409378 – H H Jul 27 '11 at 11:50
5
AppDomain.CurrentDomain.SetData("DataDirectory", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

This should work always because Directory.GetCurrentDirectory() may return other directory than the executable one

Memo
  • 329
  • 3
  • 5
1

This one solved my problem

AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory());
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Ashutosh
  • 4,371
  • 10
  • 59
  • 105