79622824

Date: 2025-05-15 07:36:52
Score: 0.5
Natty:
Report link

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(&quot;#pragma warning&quot;))"
        Lines="$([System.String]::Concat(&quot;#pragma warning disable 1591&quot;,$([System.Environment]::NewLine),$([System.Environment]::NewLine),$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath)))))"
        Overwrite="true"
        Encoding="Unicode" />
  </Target>
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @j-loscos
  • User mentioned (0): @Christoph
  • Low reputation (0.5):
Posted by: Gclpixel