Pergunta

I would like to read an option out of my appSettings.config file to create a conditional section in my CSPROJ. I know how to do the conditional references with help from visual studio 2010 conditional references but I am not sure how to access the appSettings file from within.

Is this possible and if so, could someone provide some guidance please.

EDIT Following @palo's answer I now have:

<Target Name="BeforeBuild">
        <XmlPeek XmlInputPath="SiteSettings.config" Query="appSettings/add[@key='cProjectNumber']/@value">
            <Output TaskParameter="Result" ItemName="value" />
        </XmlPeek>
        <Message Text="TESTING: @(value)" Importance="high" />
    </Target>

This works well and prints out the project number i.e Testing: 012. Now, how do I go about using it in some compile includes? I have tried:

<ItemGroup>
    <Compile Include="Accounts\@(value)\Controls\MyControl.ascx.cs">
        <SubType>ASPXCodeBehind</SubType>
    </Compile>
</ItemGroup>

But I get an error saying:

The expression "Accounts\@(value)\Controls\MyControl.ascx.cs" cannot be used in this context. Item lists cannot be concatenated with other strings where an item list is expected. Use a semicolon to separate multiple item lists.

Foi útil?

Solução 2

Following @palo's answer I came up with the following (I will mark this as the answer as it details information on how to achieve what I wanted):

<Target Name="BeforeBuild">
    <XmlPeek XmlInputPath="SiteSettings.config" Query="appSettings/add[@key='cProjectNumber']/@value">
        <Output TaskParameter="Result" ItemName="value" />
    </XmlPeek>
    <Message Text="TESTING: @(value)" Importance="high" />
    <PropertyGroup>
        <ProjectNumber>@(value)</ProjectNumber>
    </PropertyGroup>
    <ItemGroup>
        <Compile Include="Accounts\$(ProjectNumber)\Controls\MyControl.ascx.cs">
            <SubType>ASPXCodeBehind</SubType>
        </Compile>
    </ItemGroup>
</Target>

With an XML structure like:

<appSettings>
  <add key="cProjectNumber" value="123" />
</appSettings>

Outras dicas

If I understand correctly you need to read elements’ value for appconfig (xml file) and then use its value in your csproj file. Try to use XmlPeek - http://msdn.microsoft.com/en-us/library/ff598684.aspx ; How to use XmlPeek task?

Keep in mind you the order of evaluation. You cannot override global properties/items. But with msbuild 4.0 you can override items within BeforeTargets or by AfterTargets

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top