oshirowanen
oshirowanen

Reputation: 15925

Access to the database file is not allowed

I have an WinXP application (which works perfectly in WinXP) which I am trying to install on Win7. The installation was fine, but when I double click the application to load it, I get a message saying:

Access to the database file is not allowed [file path goes here].

At the moment, I have the connection set as:

SqlCeConnection("Data Source=|DataDirectory|/db.sdf")

I have also tried:

SqlCeConnection("Data Source=*|DataDirectory|*db.sdf")

and

SqlCeConnection("Data Source=*|DataDirectory|*/db.sdf")

for which I got a different error message:

The file name is not valid

Why is this happening and how do I stop this from happening?

Upvotes: 1

Views: 420

Answers (1)

Steve
Steve

Reputation: 216243

By default, the |DataDirectory| variable will be expanded as follow:

  • For applications placed in a directory on the user machine, this will be the app’s (.exe) folder.
  • For apps running under ClickOnce, this will be a special data folder created by ClickOnce
  • For Web apps, this will be the App_Data folder

In Windows7 the directory C:\program files (x86)\yourexefolder cannot be written. If you install your database in a subfolder of your application or in the same folder you get this error.

You should install your database to the %APPDATA% directory

For example in C# you can set the DataDirectory path in this way

string myDataDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\yourAppDataFolder";

// Set |DataDirectory| value 
AppDomain.CurrentDomain.SetData("DataDirectory",myDataDir);  

Upvotes: 2

Related Questions