4

For an MSBuild project, I would like to output some kind of a .config file that would be redistributed along the generated binary so the parameters used at build time can be checked by the users of the binary, programmatically.

Output file format:

PropertyName1=ValueA
PropertyName2=ValueB
...

Ideally, the list of properties to write would contain just their names. Maybe like:

<ItemGroup>
  <MyExposedDictionary Include="Configuration" />
  <MyExposedDictionary Include="Platform" />
  <MyExposedDictionary Include="PropertyName1" />
  ...
</ItemGroup>

With MyExposedDictionary being the argument to give to some DotConfigFileWriter task, as well as the path of the destination file.

I found several ways to write down values to a file, including a sub-target with some C# code in it, but I'm new to MSBuild and I'm not sure how I can merge those requirements into a single Target to make it re-usable.

Community
  • 1
  • 1
polyvertex
  • 749
  • 6
  • 15
  • 1
    Writing to a file is easy but the other problem boils down to 'given the name of a property as a string, how to get it's value'. Which unfortunately is hard, if not impossible; search around for e.g. 'msbuild custom task get property' then read e.g. http://stackoverflow.com/questions/10474990/how-do-i-access-current-project-context-within-a-custom-task. If however you're willing to add some duplication you could just use something like (or first build an itemgroup with lines, then write it to file) – stijn Feb 08 '17 at 15:23
  • @stijn that's a valuable input, thanks – polyvertex Feb 08 '17 at 15:57

2 Answers2

4

In case someone comes here with the same requirement, here is what I ended up with. Not really happy with the result as I was hoping for something more generic but at least it does the job and blends well in my project:

<Target Name="WriteBuildProperties" BeforeTargets="PreBuildEvent">
  <WriteLinesToFile File="$(DotConfigFile)" Overwrite="true" Lines="" />
  <WriteLinesToFile File="$(DotConfigFile)" Lines="ProjectName=$(ProjectName)" />
  <WriteLinesToFile File="$(DotConfigFile)" Lines="Configuration=$(Configuration)" />
  ...
</Target>

If someone happen to have a more elegant solution, please jump in!

polyvertex
  • 749
  • 6
  • 15
0

I am not sure where your problem is located. I have a similar requirement that a file is created by the program which just was compiled. I edited the properties of the project: in the build events enter a Post-build action like

REM create special file
"$(ProjectDir)$(OutDir)MyProgram.exe" /WriteFile MyFile.xml

Of course, you must also change your program such that it does the right thing when called with that parameter (and stops after having completed that action - does not show a GUI or start as a Windows Service).

Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33
  • The generated binary does not have all the required values, only the MSBuild project **at build time** does and changing the nature of the generated program here is out of question unfortunately. – polyvertex Feb 08 '17 at 11:03