An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hello @VAer-4038
A consideration is to store the path/setting in app.config and use a class to get/set the path and another class for providing access to the setting in any form or class in the application. The class RuntimeSettings is thread safe singleton.

Prerequisite
Add a reference for System.Configuration for working with settings in app.config.
Full source
https://github.com/karenpayneoregon/code-samples-csharp/tree/master/SingletonExample1
Sample app configuration
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<appSettings>
<add key="DatabasePath" value="C:\Users\QR\Documents\UserDatabase.accdb" />
</appSettings>
</configuration>
Get/Settings class
using System.Configuration;
using System.IO;
using System.Reflection;
namespace SingletonExample1.Classes
{
public class ApplicationSettings
{
public static string GetDatabasePath() => GetSettingAsString("DatabasePath");
public static void SetDatabasePath(string value) => SetValue("DatabasePath", value);
public static string GetSettingAsString(string configKey) => ConfigurationManager.AppSettings[configKey];
public static void SetValue(string key, string value)
{
var applicationDirectoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var configFile = Path.Combine(
applicationDirectoryName, $"{Assembly.GetExecutingAssembly().GetName().Name}.exe.config");
var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap,
ConfigurationUserLevel.None);
config.AppSettings.Settings[key].Value = value;
config.Save();
Reload();
}
public static void Reload()
{
ConfigurationManager.RefreshSection("appSettings");
}
}
}
Class to interact with settings.
Note the event/delegate is optional which can be used to inform all listeners who subscribe to this event that the database setting has changed.
using System;
namespace SingletonExample1.Classes
{
public sealed class RuntimeSettings
{
private static readonly Lazy<RuntimeSettings> Lazy =
new Lazy<RuntimeSettings>(() => new RuntimeSettings());
public static RuntimeSettings Instance => Lazy.Value;
public string DatabasePath
{
get => ApplicationSettings.GetDatabasePath();
set
{
ApplicationSettings.SetDatabasePath(value);
OnDatabasePathChangedEvent?.Invoke();
}
}
public delegate void OnDataPathChanged();
public static event OnDataPathChanged OnDatabasePathChangedEvent;
}
}
Get/set any where
- Get RuntimeSettings.Instance.DatabasePath
- Set RuntimeSettings.Instance.DatabasePath
Main form
using System;
using System.Windows.Forms;
using SingletonExample1.Classes; namespace SingletonExample1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}RuntimeSettings.OnDatabasePathChangedEvent += RuntimeSettingsOnOnDatabasePathChangedEvent; } private void RuntimeSettingsOnOnDatabasePathChangedEvent() { DatabasePathTextBox.Text = RuntimeSettings.Instance.DatabasePath; } private void GetDatabaseButton_Click(object sender, EventArgs e) { DatabasePathTextBox.Text = RuntimeSettings.Instance.DatabasePath; } private void SetDatabasePathButton_Click(object sender, EventArgs e) { RuntimeSettings.Instance.DatabasePath = DatabasePathTextBox.Text; } private void OpenChildForm_Click(object sender, EventArgs e) { var childForm = new ChildForm(); try { childForm.ShowDialog(); } finally { childForm.Dispose(); } } }
A child form
using System;
using System.Windows.Forms;
using SingletonExample1.Classes;
namespace SingletonExample1
{
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private void GetDatabaseButton_Click(object sender, EventArgs e)
{
DatabasePathTextBox.Text = RuntimeSettings.Instance.DatabasePath;
}
private void SetDatabasePathButton_Click(object sender, EventArgs e)
{
RuntimeSettings.Instance.DatabasePath = DatabasePathTextBox.Text;
}
}
}
Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.