1

I'm writing a program that is using the <appSettings> section of the app.config file to get the variables. Here's an example of my situation currently:

string ip =         ConfigurationManager.AppSettings["IP"];
string var1 =       ConfigurationManager.AppSettings["Var1"];
string var2 =       ConfigurationManager.AppSettings["Var2"];
string remotePath = @"\\" + ip + @"\C";

and the app.config file looks something like this:

<appSettings>
    <add key="IP" value="<ip address here>" />
    <add key="Var1" value="variable1" />
    <add key="Var2" value="variable2" />
</appSettings>

This all works fine and I'm okay with keeping it this way. My problem is simply that I think it looks ugly. I've been looking around to find out if keys can be nested so I can do something like this with another key:

<add key="RemotePath" value="\\" + IP + "\C" />

I know variables aren't easily possible in app.config, and my problem isn't really big enough to make me interested in doing a lot of work, I'm just interested if key nesting is possible? I haven't been able to find any hint of a yes or no from Google.

Community
  • 1
  • 1
Alamb
  • 137
  • 10
  • Well, the last example isn't valid XML. That `value` attribute would have to be some static value. So any resolution of "variables" or other placeholders in a value would need to be performed in code. I don't think there's any built-in mechanism to do that, but it wouldn't be terribly difficult to write one. You can wrap your config value access in a class which would internally look for placeholder syntax (which you'd define, perhaps something like `value="\\{{IP}}\C"` or however you want to define it) and perform the string replace. – David Apr 06 '16 at 16:15

0 Answers0