如果您的文件仅包含这些行,也许您可以执行以下操作:
<WriteLinesToFile File="$(VersionFilePath)" Lines="public const string SERVICE_VERSION = %22$(Version)%22%3B" Overwrite="true" Encoding="ASCII"/>
请注意, " 应替换为 %22 和 ; 替换为 %3B (当您添加 ';' 时,将插入一个新行字符)。
否则,您可以在 msbuild 中编写 C# 代码来执行您想要的操作。一个例子:
<UsingTask TaskName="UpdateApplicationRevision" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<ProjectFilePath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.dll" />
<Reference Include="System.Xml" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var project = new Microsoft.Build.Evaluation.Project(ProjectFilePath);
var property = project.GetProperty("ApplicationRevision");
property.UnevaluatedValue = "" + (System.Int32.Parse(property.EvaluatedValue) + 1);
project.Save();
]]>
</Code>
</Task>
</UsingTask>
在这里,我修改了一个 csproj (xml) 文件,但您可以随意编写自己的代码来读取您的文件并对其进行更新...