Using Chinese characters in .csproj can cause errors during Jenkins CICD.
Microsoft.WinFX.targets(439,8): error : Invalid character in the given encoding.
The reason is that the encoding used by MSBuild or dotnet publish is UTF-8.
When saving the .csproj file, it should be saved with UTF-8 BOM, and the characters inside should be in UTF-8.
The default encoding in PowerShell is GB2312, which can lead to this error.
Another issue is that there is a need to dynamically modify the assembly name during CICD:
<AssemblyName>Assembly Name</AssemblyName>
$content = Get-Content -Path "${env:P_NAME}.csproj" ;
$updatedContent = $content -replace '(?<=<AssemblyName>).+?(?=</AssemblyName>)', ${env:APP_NAME} ;
Set-Content -Path "${env:P_NAME}.csproj" -Value $updatedContent ;
However, since the default encoding in PowerShell is GB2312, directly replacing the text will cause the file to remain UTF-8, but the replaced characters will be in GB2312, leading to compilation errors.
The method found online is to use chcp
to change the current terminal code page, which actually has no effect.
chcp 65001 ;
The correct approach is to use -encoding utf8
when reading and writing text, ensuring that the encoding is set to UTF-8.
$content = Get-Content -Path "${env:P_NAME}.csproj" -encoding utf8;
$updatedContent = $content -replace '(?<=<AssemblyName>).+?(?=</AssemblyName>)', ${env:APP_NAME} ;
Set-Content -Path "${env:P_NAME}.csproj" -Value $updatedContent -encoding utf8;
In this way, both reading and modifying will pass the text using UTF-8 encoding.
文章评论