Karl Green
Karl Green

Reputation: 271

Building my project sets the database to read only?

I have developed a program in Visual Studio using VB that connects to a .db database. When I debug it, it works fine as the database on my computer is not set to read only (I checked by right clicking the file and going to properties).

I use the standard Visual Studio set up project to build it and the read only property of the database in the set up project is set to false. However when I build the project and install it on another machine, the database somehow gets set to read only and I get the error "Attempt to write a read-only database"

Does anybody know how I would go about solving this? Thanks

Edit: Hmm I have just checked and the folder that the database and other program files gets added to is marked as "Read only (only applies to files in the folder)". So whilst the database is not marked as read only, the folder is. Perhaps because it is in the program files folder?

Upvotes: 2

Views: 1664

Answers (1)

Steve
Steve

Reputation: 216293

The folders C:\PROGRAM FILES or C:\PROGRAM FILES (x86) are configured by the operating system (Vista and after), for security reasons, to block write operations.
The recommended folder for writing application wide data (settings or database data files) can be retrieved calling Environment.GetSpecialFolder using the parameter CommonApplicationData of Environment.SpecialFolder enum.

The standard setup tool used in Visual Studio 2010 has no predefined folder that points to this CommonApplicationData directly, however you could use a workaround:

  • Right click on the File System, select Add a Special Folder and then Custom folder
  • Give a name to the new folder in the File System Tree
  • Set the property DefaultLocation to [CommonAppDataFolder]
  • Create a subfolder for your application with a right click on the new added folder Custom Folder
  • Use this folder to add your database file.

After this, do not forget to change your application ConnectionString and point it to the new location of the file. In C# you write something like this

string commonData = ENvironment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string pathToDBFile = Path.Combine(commonData, "MyAppDir", "MyDB.sqlite");

Upvotes: 3

Related Questions