首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

NAnt 編譯帶資源文件的 C# 專案完整指南

     編輯:關於.NET

情況一:多語言專案編譯(衛星組件)

資源文件需先用 <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 讀取。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved