1

Here we have a winforms desktop application used by various users. Each user runs the application locally. That said the build folders are separate for each user. They also have different set of config keys under their local application config files to run the app (different connection strings and key options). You may guess how painful updating versions is.

My purpose is to put this winforms desktop application in a shared folder and share it to each user. The challenge here is their configurations being separate.

I tried merging the separate keys with different names and using conditions under the code but the job seems massive.

Is there any way to collect all these separate config files under the bin folder and switch between them depending on the username? What's the easiest way approaching this problem?

Thank you.

Baz Guvenkaya
  • 1,482
  • 3
  • 17
  • 26
  • Does the application use a database connection? It would be good to configure all the user specific values in a central database table and have them mapped when your app initializes. – Souvik Ghosh Nov 24 '16 at 06:01
  • I suggest Portable SQLite database, for such case to store setting for each user. Note: Application need access permission to read write SQLite database – Gaurav P Nov 24 '16 at 06:08
  • @SouvikGhosh thanks for your suggestion. Even the DB keys have different values due to different connection drivers per computer so it might not be the best approach. – Baz Guvenkaya Nov 25 '16 at 00:46

1 Answers1

0

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.
Community
  • 1
  • 1
Michał Komorowski
  • 6,198
  • 1
  • 20
  • 24
  • Thank you for the detailed suggestion. I will try this approach. I'll keep you posted and decide on marking your answer accordingly. – Baz Guvenkaya Nov 25 '16 at 00:48
  • I followed a different approach. I didn't go through these steps. I will accept the answer for your efforts. Thank you. – Baz Guvenkaya Dec 15 '16 at 23:30