views:

515

answers:

3

I have two projects in a Visual Studio solution. I need an application setting in one of them to be the version number of the other project's assembly.

I thought of adding to my project1's app.config something like:

<setting name="currentVersion" serializeAs="String">
  <value>
    !(bind.project2.Assembly.FileVersion)
  </value>
</setting>

but obviously this code does not work

I could change the value manually everytime I compile a new version.. but...you know...one forgets.

+1  A: 

Until you get to Visual Studio 2010, there is no ability to substitute into a config file. Then, it's only there for web.config.

John Saunders
Actually, I heard from the VS 2010 team that the substitution subsystem should be available for all configs - not just web.config
marc_s
Cool! I didn't know that.
John Saunders
A: 

You can add app.config for project1 to project2 as a file reference. Simply add app.config as an existing item to project2 but instead of clicking on 'Add', click on the little down arrow on 'Add' button to bring up a list of options and select 'Add As Link' from that list. This way a single app.config file will be shared by project1 and project2. Note that this will cause project2 to take a dependency on the location of app.config file. So you need to be careful if you want to use project2 in another solution that does not require project1.

If you don't want to share the entire app.config between two projects, you can take a look at extracting application settings into an external config file and reference it from your app.config files via configSource. However, if I am not mistaken, the external config file needs to be in the same directory or the subdirectory of app.config file. So this may require a pre-build step in project2 or both to copy that external file from either a shared location or project1.

Mehmet Aras
Nevermind. I see what you mean after your edit. I don't think that the .NET configuration framework as it exists allows that kind of binding like expressions in config files. Even if it did, what you want seems like something that you would still have to write custom code to do it.
Mehmet Aras
+1  A: 

We use NAnt to build our applications. This may be overkill for your problem, especially if you don't already use NAnt to build your app. But there is a nant task called xmlpoke that can be used to set values in any xml file at build time. E.g.: part of my NAnt build script looks like this:

<target name="updateconfig">
  <!-- Grab the mySvcSoap value from the already deployed
       appropriate config file -->
  <xmlpeek
    file="${deployment.dir}/Config/appSettings.config"
    xpath="/appSettings/add[@key = 'MyServiceSoap']/@value"
    property="MySvcSoap"/>
  <echo message="MyServiceSoap = ${MySvcSoap}"/>
  <!-- Stick the peeked value into the web.config file system.serviceModel
       configuration. -->
  <xmlpoke
    file="${deployment.dir}/web.config"
    xpath="/configuration/system.serviceModel/client/endpoint[@name = 'MyServiceSoap']/@address"
    value="${MySvcSoap}" />

</target>

You can get more info from the NAnt xmlpoke documentation page.

Ogre Psalm33