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
  • 5,844
  • 14
  • 40
  • 71

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.

Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
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
  • 321
  • 3
  • 4
1

This one solved my problem

AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory());
kleopatra
  • 50,242
  • 28
  • 96
  • 201
Ashutosh
  • 3,945
  • 9
  • 53
  • 92