0

We have a c# webform application that has AppSettings and database ConnectionStrings configSource'd out into separate files.

<appSettings configSource="Customisations/AppSettings.config" />

I've been asked to allow end-users to connect to different databases and use different app settings based on which app shortcut they use.

I was hoping to be able to allow end-users to effectively override the configSource by adding something like the argument below to an app shortcut's target line.

myApp.exe appSettings='\\unc-path\app-settings-test.config'

Will this be possible? Are there any security concerns, i.e. UAC?

If ok... How do I make the app load the AppSettings and ConnectionStrings from these overridden locations but leave the current config file intact?

Drammy
  • 940
  • 12
  • 30

1 Answers1

0

I would suggest to pass the config file (incl. path) to string[] args and read it before you open the config file. However, concerning the security aspect you should check on certain Syntax etc. e.g. if the path includes ";" or ")" and so on... or you can check via regexp, if the given path has the correct structure. Anyways, search Google (or this Forum... Google will lead you here anyways ;) ) for string[] args.

Tyron78
  • 4,117
  • 2
  • 17
  • 32
  • Thanks Rene, yeah I know how to pass arguments - I'm asking if it is possible to override the ConfigurationManager's ConfigSection's ConfigSource at run time... Apologies if that wasn't clear – Drammy Jul 04 '16 at 11:07
  • Oh, OK. Sorry - misunderstood the question. In this case you might try "AppDomain.CurrentDomain.SetData" - this SHOULD help to set th epath of the config file. I found this Statement somewhere here on StackOverflow when searching for a similar issue. – Tyron78 Jul 04 '16 at 11:17
  • Here is the post I meant: http://stackoverflow.com/questions/1838619/relocating-app-config-file-to-a-custom-path – Tyron78 Jul 04 '16 at 11:18
  • Thanks again, I don't want to change the config file, I want to programmatically override just the connectionStrings and appSettings ConfigSections' ConfigSource(s). I've been searching google and SO all morning before posting a new question - I can't find anything similar to what I want to do. I've done it programmatically now; just wondered if it was possible through config – Drammy Jul 04 '16 at 11:29
  • Damn me - misunderstood you again!? :-( I understood your mentioned example (appSettings = ...config" as assignment of a new config file... appSettings is what the config is about, no? Concerning the connectionstring let me try once more. ;-) I would suggest to pass something like "connString=user@db" into args. Then analyze the providet arguments and - if "connString=" is included extract the value. When creating the Connection, first check if you received a connString from args. If so, use this Connection string, if not take the one from the config file. But anyways: glad you could solve it. – Tyron78 Jul 04 '16 at 11:49
  • 1
    No worries, I obviously wasn't clear enough to start with :) Yeah the conn string approach you suggested is pretty much what I did for both. I just abstracted the AppSettings access and put the argument detection there, same for the ConnectionString retrieval. I'll add the code I came up with at some point for future reference. – Drammy Jul 04 '16 at 14:57