Thank you for the inspiration. @j-loscos and @Christoph
I have some projects without any designer file in my solution and add the target into my Directory.Build.targets file.
The compiler will fail with "[System.IO.File]::ReadAllText().StartsWith("#pragma warning")". Method 'System.IO.File.ReadAllText' not found. Static method invocation should be of the form: $([FullTypeName]::Method()), e.g. $([System.IO.Path]::Combine(a
, b
)). Check that all parameters are defined, are of the correct type, and are specified in the right order."
so I change the condition to validates whether the msbuild variable is not empty.
%(AutoGeneratedFiles.FullPath) != ''
the modified complete msbuild target snippet:
<Target Name="DisableWarnings" BeforeTargets="CoreCompile">
<ItemGroup>
<AutoGeneratedFiles Include="**/*.Designer.cs" />
</ItemGroup>
<WriteLinesToFile File="%(AutoGeneratedFiles.FullPath)"
Condition="%(AutoGeneratedFiles.FullPath) != '' and !$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath)).StartsWith("#pragma warning"))"
Lines="$([System.String]::Concat("#pragma warning disable 1591",$([System.Environment]::NewLine),$([System.Environment]::NewLine),$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath)))))"
Overwrite="true"
Encoding="Unicode" />
</Target>