資源文件需先用 <resgen> 轉換為 .resources,再用 <al> (Assembly Linker) 生成衛星組件,最後 <csc> 編譯主程式並嵌入預設文化特性資源。
<!-- 轉換資源文件 -->
<resgen input="ResourceText.resx" output="${nant.project.name}.ResourceText.resources" todir="${build.dir}\bin" />
<resgen input="ResourceText.zh-CHS.resx" output="${nant.project.name}.ResourceText.zh-CHS.resources" todir="${build.dir}\bin\zh-CHS" />
<resgen input="ResourceText.en-US.resx" output="${nant.project.name}.ResourceText.en-US.resources" todir="${build.dir}\bin\en-US" />
<!-- 編譯衛星組件:簡體中文 -->
<al output="${build.dir}\bin\zh-CHS\${nant.project.name}.resources.dll" target="lib" culture="zh-CHS">
<sources basedir="${build.dir}\bin\zh-CHS">
<includes name="${nant.project.name}.ResourceText.zh-CHS.resources" />
</sources>
</al>
<!-- 編譯衛星組件:美式英文 -->
<al output="${build.dir}\bin\en-US\${nant.project.name}.resources.dll" target="lib" culture="en-US">
<sources basedir="${build.dir}\bin\en-US">
<includes name="${nant.project.name}.ResourceText.en-US.resources" />
</sources>
</al>
<!-- 編譯主程式 -->
<csc warnaserror="true" debug="${build.debug}" doc="${build.dir}\bin\${nant.project.name}.xml" output="${build.dir}\bin\${nant.project.name}.exe" target="winexe" win32icon="App.ico">
<sources failonempty="true">
<includes name="**\*.cs" />
<includes name="..\CommonAssemblyInfo.cs" />
</sources>
<resources basedir="${build.dir}\bin">
<includes name="${nant.project.name}.ResourceText.resources" />
</resources>
</csc>
若資源文件命名符合 VS.NET 自動生成規則(如 Form1.resx 對應 Form1.bmp),勿使用 <resgen>,直接在 <csc> 的 <resources> 區塊引用,編譯器自動處理。
<target name="build">
<echo message="編譯${nant.project.name}專案" />
<csc warnaserror="true" debug="${build.debug}" doc="${build.dir}\bin\${nant.project.name}.xml" output="${build.dir}\bin\${nant.project.name}.dll" target="library">
<sources failonempty="true">
<includes name="**\*.cs" />
<includes name="..\CommonAssemblyInfo.cs" />
</sources>
<resources basedir="." prefix="${nant.project.name}" dynamicprefix="true">
<includes name="Arrows\*.gif" />
<includes name="CheckBoxes\*.bmp" />
<includes name="RadioButtons\*.gif" />
</resources>
</csc>
</target>
basedir=".":資源文件相對路徑基準prefix="${nant.project.name}":資源命名前綴(對應預設命名空間)dynamicprefix="true":根據目錄結構自動調整前綴,支援巢狀資源此方式讓編譯器自動將圖片嵌入組件清單,運行期透過 GetManifestResourceStream 讀取。