ファイルtest.xmlの「how r u」などの文字列を、別のファイルxy.xml.using正規表現の文字列「i am fine」に置き換えたいです。
つまり、あるファイル (xy.xml) から文字列を読み取り、それを別のファイル test.xml に置き換える必要があります。そのため、この問題を解決するために必要な手順を例とともに提供してください
ファイルtest.xmlの「how r u」などの文字列を、別のファイルxy.xml.using正規表現の文字列「i am fine」に置き換えたいです。
つまり、あるファイル (xy.xml) から文字列を読み取り、それを別のファイル test.xml に置き換える必要があります。そのため、この問題を解決するために必要な手順を例とともに提供してください
これはもう必要ありません...C# をプロジェクト/ビルド ファイルに挿入できるようになりました...
カスタム タスクとパラメータを次のように定義します。
<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<OutputFilename ParameterType="System.String" Required="true" />
<MatchExpression ParameterType="System.String" Required="true" />
<ReplacementText ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
File.WriteAllText(
OutputFilename,
Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
);
]]>
</Code>
</Task>
</UsingTask>
次に、他の MSBuild タスクと同じように呼び出すだけです
<Target Name="AfterBuild">
<ReplaceFileText
InputFilename="$(OutputPath)File.exe.config"
OutputFilename="$(OutputPath)File.exe.config"
MatchExpression="\$version\$"
ReplacementText="1.0.0.2" />
</Target>
上記の例では、出力ディレクトリにある「File.exe.config」の「$version$」を「1.0.0.2」に置き換えます。
記事http://geekswithblogs.net/mnf/archive/2009/07/03/msbuild-task-to-replace-content-in-text-files.aspxで説明されているように、MSBuild Community Tasks のタスク FileUpdate を使用できます。
Unix ドライブにある同じファイルに対して両方の置換を実行し、\server\path... への unc パスを使用しました。
<ReplaceFileText
InputFilename="$(fileToUpdate)"
OutputFilename="$(fileToUpdate)"
MatchExpression="15.0.0"
ReplacementText="15.3.1"/>
<FileUpdate Files="$(fileToUpdate2)"
Regex="15.0.0"
ReplacementText="15.3.1" />
また、上記の cs カスタム アクションは bom を追加しません。ただし、FileUpdate は次のことを行いました。
%head -2 branding.h branding2.h
==> branding.h <==
#/* branding.h
#** This file captures common branding strings in a format usable by both sed and C-preprocessor.
==> branding2.h <==
#/* branding.h
#** This file captures common branding strings in a format usable by both sed and C-preprocessor.
csharptest.net に感謝します - UNIX ビルド用の perl 置換コマンドを使用して exec を実行していました。