Question

I'm using the settings editor in Visual Studio 2010 to add/edit/remove my settings from the section of my .NET 4 web.config file.

Here's a (pretty vague) example extract to illustrate the bit I mean:

  <applicationSettings>
    <Animals.My.MySettings>
      <!-- Specify the type of animal this website is dedicated to... -->
      <setting name="AnimalType" serializeAs="String">
        <value>Monkey</value>
      </setting>
    </Animals.My.MySettings>
  </applicationSettings>

In the example above, the comment is useful for anyone manually editing the config file on-site (e.g. once the web application has been deployed to the customer site). However, if I use the VS settings editor, the comment is lost whenever I add/edit/remove a setting.

So far, I've come up with the following work-arounds:

  1. I could choose to never use the VS settings editor, but I'd have to tell my team to avoid it too, and there's always the one time that someone forgets and we lose all our comments...

  2. I could keep a separate copy of the web.config, with a bunch of comments in it. We then ship the application with the copy of the config file... (I don't like this idea, because it means I've got to remember to keep the second copy up-to-date and I have to remember to switch the config files on release... Too much to remember; too much could go wrong).

  3. Adding comments above the opening tag seems to be OK, so I could just have all my comments at the top.

My question is: What's the best way to work around this problem? What do you recommend?

Was it helpful?

Solution

Option 1 and 2 suffer from the same problem: team discipline. 1 means they can't use the studio editor, and 2 means they have to remember to keep the "comment" config in sync.

Personally, I think the first one is easier to enforce and if you are using source control, then it's pretty obvious who the offenders are.

OTHER TIPS

I would recommend that you look at generating all of your environment-specific config, including environment-specific comments, using ConfigGen. The examples on this page might give you some good ideas.

Essentially, it gives you a single place to edit your config for all of your environments, using tokenised placeholders and a settings file with a row for each deployment environment or dev machine. You can include/exclude comments on a machine by machine basis or by using a boolean condition.

Also, take a look at the example of usage I posted in this answer...

How to select different app.config for several build configurations

My workaround -- here is part of a .config file with comments:

<setting name="Database" serializeAs="String">
<value>DBNAME</value>
</setting>
<setting name="ProgramPath" serializeAs="String">
<value>C:\Program Files (x86)\Vendor\Program V 11.1\prog.exe:: the P21 client -  KEEP SPACE in a comment will not remove spaces from the value</value>
</setting>
<setting name="Directory" serializeAs="String">
<value>\\Server\Share\Subdir :: where the files will be created</value>
</setting>
<setting name="FileIDs" serializeAs="String">
<value>DoThis, ThenThis :: DoThis or ThenThis or both comma separated - anything after two colons is a comment and is ignored</value>
</setting>

This VB.NET function strips the comments and by default removes embedded spaces

Function Run(S As String) As String
    Dim L As Integer = InStr(S, "::")
    If L = 0 Then
        Return S.Replace(Space(1), String.Empty)
    Else
        If InStr(S, "KEEP SPACE") = 0 Then
            Return S.Substring(0, L - 1).Replace(Space(1), String.Empty)
        Else
            Return S.Substring(0, L - 1)
        End If
    End If
End Function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top