My proposition is to use a modified .NET settings mechanism. In general you can have application specific and user specific settings. In this case we would like to use the user settings.
The problem with this approach is that user settings are not stored in the bin folder but somewhere else i.e. %userprofile%\appdata\local... (here are details). However, you can override this behaviour by writing a custom settings provider. Luckily someone has already did it. CustomSettingsProvider
from this answer works like a charm.
To sum here is a solution:
- In order to add a settings file to you project right click the
project and then select Add -> New item... Finnally, find
Settings File template. If you double click the added file a designer will pop up. Let's assume that this file is called MySettings.
Define some user specific settings.
Add CustomSettingsProvider
to your project.
Find MySettings
class in the project and add the following attribute to it:
[System.Configuration.SettingsProvider(typeof(CustomSettingsProvider))]
.
- Before using settings you have to specify a path to a file with them. I suggest to do it as the one of the first things in your application. In the following example settings files be created in a current folder and their names will contain a user name:
MySettings.Default.SettingsKey = $@".\user_{Environment.UserName}.config";
- If a file with settings does not exist
CustomSettingsProvider
will create an empty one.
- When closing the application call MySettings.Default.Save(); to save user settings to a file.
- Run your application once on your own just to create a template file. Then make a copy of this template for other users changing their names accordingly.
- The disadvantage of my approach is that you will have no connection strings section in settings file. So reference to
ConfigurationManager.ConnectionStrings
you will have to replace with references to MySettings.PropertyWithConnectionString
.