{"id":107974,"date":"2023-03-27T07:00:00","date_gmt":"2023-03-27T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107974"},"modified":"2023-03-26T19:50:17","modified_gmt":"2023-03-27T02:50:17","slug":"20230327-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230327-00\/?p=107974","title":{"rendered":"Using the contents of a file to define an MSBuild property"},"content":{"rendered":"<p>Say you want to define a property in your MSBuild project file (vcxproj, csproj, etc.) and have the property value come from a file. You might try using the <code>Read\u00adLines\u00adFrom\u00adFile<\/code> task to get the contents:<\/p>\n<pre>  &lt;Target Name=\"DefineMagic\"&gt;\r\n    &lt;ReadLinesFromFile File=\".\\magic.txt\"&gt;\r\n      &lt;Output TaskParameter=\"Lines\" PropertyName=\"Magic\" \/&gt;\r\n    &lt;\/ReadLinesFromFile&gt;\r\n  &lt;\/Target&gt;\r\n  &lt;ItemDefinitionGroup&gt;\r\n    &lt;ClCompile&gt;\r\n      &lt;PreprocessorDefinitions&gt;MAGIC=\"$(Magic)\";%(PreprocessorDefinitions)&lt;\/PreprocessorDefinitions&gt;\r\n    &lt;\/ClCompile&gt;\r\n  &lt;\/ItemDefinitionGroup&gt;\r\n<\/pre>\n<p>This doesn&#8217;t work because the <code>Target<\/code> doesn&#8217;t run until after the <code>Item\u00adDefinition\u00adGroup<\/code> is already defined. MSBuild evaluates <code>Property\u00adGroup<\/code> and <code>Item\u00adGroup<\/code> elements before running any <code>Target<\/code>s, By the time you execute the <code>Read\u00adLines\u00adFrom\u00adFile<\/code> task, it&#8217;s too late.<\/p>\n<p>Instead, you can use the <code>Read\u00adAll\u00adText<\/code> <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/msbuild\/property-functions?view=vs-2022\"> MSBuild property function<\/a> to read the text into a property.<\/p>\n<pre>  &lt;PropertyGroup&gt;\r\n    &lt;Magic&gt;$([System.IO.File]::ReadAllText('.\\magic.txt').TrimEnd())&lt;\/Magic&gt;\r\n  &lt;\/PropertyGroup&gt;\r\n  &lt;ItemDefinitionGroup&gt;\r\n    &lt;ClCompile&gt;\r\n      &lt;PreprocessorDefinitions&gt;MAGIC=\"$(Magic)\";%(PreprocessorDefinitions)&lt;\/PreprocessorDefinitions&gt;\r\n    &lt;\/ClCompile&gt;\r\n  &lt;\/ItemDefinitionGroup&gt;\r\n<\/pre>\n<p>You can see an example of this trick in the <a href=\"https:\/\/github.com\/microsoft\/WindowsAppSDK\/blob\/0dc4d2527a326fda218fe543d98f2fa4bc54357e\/Directory.Build.props#L62\"> WindowsAppSDK build properties<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little trick that maybe should be better known.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-107974","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A little trick that maybe should be better known.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=107974"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107974\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=107974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}