Hello guys
I'm developing a small WFP + C# application that has a copy of Sqlite database for individual user preferences.
How do I insure that the software copies the database to individual user's AppData folder (eg: C:\Users\rajesh\AppData\Roaming\MySoftware\my.db) during the first run from the current installation path?
Please help


Amira BedhiafiRank #601 · 4K reputationPosted Mar 22, 2025, 11:09 AM EDT
You can use the Environment.GetFolderPath method to get the path to the AppData folder for the current user.
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string appFolder = Path.Combine(appDataFolder, "MySoftware");You need to check if the folder exists, and if not, create it.
if (!Directory.Exists(appFolder)) { Directory.CreateDirectory(appFolder); }Then define the path where the database will be copied to in the AppData folder.
string destinationDbPath = Path.Combine(appFolder, "my.db");If the database file already exists in the AppData folder, you might not want to overwrite it. You can check for its existence before copying.
if (!File.Exists(destinationDbPath)) { // Copy the database from the installation path to the AppData folder string sourceDbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "my.db"); if (File.Exists(sourceDbPath)) { File.Copy(sourceDbPath, destinationDbPath); } else { // Handle the case where the source database does not exist MessageBox.Show("Source database not found!"); } }From this point onward, your application should use the database from the AppData folder.
string connectionString = $"Data Source={destinationDbPath};Version=3;";