共用方式為


HOW TO:存取附屬 DLL 中的資源

一旦已經建立附屬 DLL 並將資源加入至其中 (圖示、點陣圖、資源字串等等),便可在您的增益集和其他 Automation 專案中使用那些資源。 下列程序會示範如何執行此項作業。

注意事項注意事項

根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。這些程序已使用現行的 [一般開發設定] 進行開發。 若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

存取附屬 DLL 資源

  1. 開啟 Visual Studio,然後載入現有的增益集專案,或是建立新的專案。

  2. 加入下列程式碼範例,進行編譯並加以執行。

範例

下列是 Visual Studio 用來尋找附屬 DLL 的一般演算法。 您可以使用此程式碼,以確定附屬 DLL 是否適當地建置、位於正確的位置,以及是否具有您預期的資源名稱。

static void Main(string[] args)
{
    string path = @"<some path here>";
    System.Reflection.Assembly asm =    
    System.Reflection.Assembly.LoadFrom(path);
    // For enhanced security, use the LoadFrom overload 
    // System.Reflection.Assembly.LoadFrom(path, securityInfo);
    // where securityInfo is an instance of an Evidence object.
    System.Reflection.Assembly assemblyForResources = 
    asm.GetSatelliteAssembly(System.Threading.
    Thread.CurrentThread.CurrentCulture);
    System.IO.Stream stream =    
    assemblyForResources.GetManifestResourceStream
    (assemblyForResources.GetManifestResourceNames()[0]);
    ResourceReader resReader = new ResourceReader(stream);
    foreach (System.Collections.DictionaryEntry entry in resReader)
    {
        System.Windows.Forms.MessageBox.Show(entry.Key.ToString());
    }
}

編譯程式碼

若要使用這個範例,請建立 Visual C# 主控台應用程式 (Console Application)、加入此程式碼以取代 Main() 函式,並將路徑變數設定為增益集組件 (Assembly) 的路徑 (而非附屬 DLL 的路徑)。 在執行時,您將會在附屬 DLL 中看見所有可用的資源。

請參閱

工作

逐步解說:建立 Managed 附屬 DLL