using System.IO; namespace UtopiaCanteenSystem.Data; /// /// Central location for persistent paths (database and config). Uses LocalApplicationData /// so data survives app updates when the application is deployed from a file server. /// public static class DatabasePath { private static string? _appDataFolder; private static string? _dbPath; /// /// Folder under LocalApplicationData for DB and config. Created on first use. /// public static string GetAppDataFolder() { if (_appDataFolder != null) return _appDataFolder; _appDataFolder = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UtopiaCanteenSystem"); Directory.CreateDirectory(_appDataFolder); return _appDataFolder; } /// /// Full path to the SQLite database file. If a DB exists in the legacy app-directory /// location, it is copied to the new location once. /// public static string GetDbPath() { if (_dbPath != null) return _dbPath; var folder = GetAppDataFolder(); _dbPath = Path.Combine(folder, "utopia_canteen.db"); MigrateLegacyDbIfNeeded(_dbPath); return _dbPath; } /// /// Full path to appsettings.json (remember-me credentials and other config). /// Stored in the same persistent folder so it survives updates. If a config exists /// in the legacy app-directory location, it is copied to the new location once. /// public static string GetConfigPath() { var folder = GetAppDataFolder(); var newPath = Path.Combine(folder, "appsettings.json"); MigrateLegacyConfigIfNeeded(newPath); return newPath; } private static void MigrateLegacyDbIfNeeded(string newPath) { if (File.Exists(newPath)) return; var legacyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "utopia_canteen.db"); if (!File.Exists(legacyPath)) return; try { File.Copy(legacyPath, newPath); } catch { // If copy fails, app will start with a new DB at new path. } } private static void MigrateLegacyConfigIfNeeded(string newPath) { if (File.Exists(newPath)) return; var legacyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"); if (!File.Exists(legacyPath)) return; try { File.Copy(legacyPath, newPath); } catch { // If copy fails, app will start with a new config at new path. } } }