I have a C# Winforms application using SQLite as a database. Since SQLite is serverless, there's an actual database file.
I have the following app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Default" connectionString="Data Source = ./Data/MyDb.db;Version=3;foreign keys=true" providerName="System.Data.SQLite.Core"/>
<add name="ConstraintsOff" connectionString="Data Source = ./Data/MyDb.db;Version=3;" providerName="System.Data.SQLite.Core"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
One of the functionalities of my app is to allow the user to backup their database, so naturally I also need a functionality that will let them mount that backup in the event they need to.
I know how to create a file selector in UI and to fetch a file from there, but I don't know how to programmatically change the value of connectionString
and persist the changes on the file.
I have read this question but the issue seems a bit different there, since it doesn't look like these people are modifying the app.config file but some other configuration file.
I have
ConfigurationManager.ConnectionStrings["Default"].ConnectionString = "Data Source = " + newDbPath + ";Version=3;foreign keys=true";
ConfigurationManager.ConnectionStrings["ConstraintsOff"].ConnectionString = "Data Source = " + newDbPath + ";Version=3";
But these changes will not get saved into the file, so the user will always have to manually mount the DB every time they open it.
As seen in the code, app.config has an entry which in turn has 2 entries added to it, I don't know how to "go down" 2 levels, so I can only reach the value for <connectionStrings>
I'd also like to know if its possible to store a date in app.config, check it and update it programatically as well. I am going to implement a backup functionality that will run every 30 days, and i dont wanna have to set up a windows service so storing a "last date of backup" works for me too.