0

I am using Microsoft Visual Studio Installer Projects to make a setup file for my C# app and when I install my app it says:

Failed to update database because the database is read-only

one

Here is screenshot of my database in Microsoft SQL Management Studio that shows read-only is false.

two

Here is screenshot of my database file in installer that shows read-only is false.

three

Yet after installing my app I get error above.

Any idea?

Dale K
  • 25,246
  • 15
  • 42
  • 71
mafortis
  • 6,750
  • 23
  • 130
  • 288
  • Move your database to AppData\Local (`Application.LocalUserAppDataPath`) or AppData\Roaming (`Application.UserAppDataPath`) or probably ProgramData (`Application.CommonAppDataPath`). Predefined locations where your app always has all access rights. – Jimi Aug 04 '21 at 09:23
  • @Jimi so in that case I have to move my main db (the one I use for debug) to that location as well right? Because if not then sql requests either are set to current path while debugging but when I create setup file my db will be in other path (AppData for instance, then it cannot find it) – mafortis Aug 04 '21 at 09:32
  • @Jimi or maybe you have solution to this path difference? – mafortis Aug 04 '21 at 09:33
  • The database location is often defined in the application setup file(s). See, e.g, [Where is |DataDirectory| defined?](https://stackoverflow.com/q/12187068/7444103). `|DataDirectory|` specifies the location, which can be changed to something else when the app is deployed. – Jimi Aug 04 '21 at 09:41
  • @Jimi that's what I'm saying, right now db file is in app folder, so all codebase (app.config) is set to that path (even after creating install file) now if I define my db file to go in AppData folder after install it means I must change my app.config settings which will work after installation but cause issue on debug. – mafortis Aug 04 '21 at 09:43
  • Are you using the `|DataDirectory|` feature? You can redefine it when the app is installed (or first run). See that SO Q&A post and [AppDomain.CurrentDomain.SetData()](https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.setdata) – Jimi Aug 04 '21 at 09:46
  • @Jimi yes I'm using data directory, – mafortis Aug 04 '21 at 09:58
  • @Jimi can you tell me how to define different path by mentioned link? (Still newbie :/ ) and how it detect which path use on debug and which to use after install? – mafortis Aug 04 '21 at 10:01
  • It's explained in that post and many others, e.g., [ADO.NET |DataDirectory| where is this documented?](https://stackoverflow.com/q/1409358/7444103) and [Where is DataDirectory?](https://social.msdn.microsoft.com/Forums/en-US/dc31ea59-5718-49b6-9f1f-7039da425296/where-is-datadirectory-?) etc. – Jimi Aug 04 '21 at 10:12
  • Or [local database not updating when using |DataDirectory|](https://stackoverflow.com/q/53643610/7444103) or [How do I read the current path of |DataDirectory| from config settings](https://stackoverflow.com/q/12266924/7444103) (many others). -- Note that all Paths, when your app is installed, are relative to the installation path or any other paths where your data is stored in the hosting machine, not just one file. You need to plan for that in any case. – Jimi Aug 04 '21 at 10:27
  • @Jimi you got me wrong, my question was if change my `DataDirectory` value by mentioned link it might work on installed app, but when I'm debugging it supposed to get my db path from my project path where sln file is (on debug there is no database on any custom path that I might set by `AppDomain.SetData` how do I tell my app the difference between debug app (still developing) and installed app to know where to look for db file? – mafortis Aug 04 '21 at 10:32
  • @Jimi thank you for your help I managed to fix the issue and shared the solution – mafortis Aug 04 '21 at 10:48
  • As mentioned, this applies to all your files that need write access or any file you don't want to keep in the installation path of your application. So you always use relative Paths (*relative* means defined by the app configuration, not *partial paths*). That's what `|DataDirectory|` is about, but it applies to all other files (or other *location* types - could be an IP Address). An Installer can be configured to deploy your files in different paths. Most often not the same Paths used in development. Your app needs to consider and handle this. – Jimi Aug 04 '21 at 10:48
  • You can *exercise* this behavior by setting data paths to `Application.CommonAppDataPath` on startup and make your app work with this relative path from the beginning -- I have no idea how your app is working, whether you're using the VS automatic tools to setup your connections / file access or whatever. In any case, you need to setup your app to handle relative locations. – Jimi Aug 04 '21 at 10:48

1 Answers1

-1

Solved

While comments above were helpful but easiest solution for me that didn't require lots of editing and coding was to change Application Folder path to outside of Program Files (86) folder something like this

C:\[Manufacturer]\[ProductName]

Here is screenshot of where to change that path and everything works fine now

one

Now it will install on path like C:\My_Company\App_Name where there is no permission restrict for system to access database file.

mafortis
  • 6,750
  • 23
  • 130
  • 288