2009-04-21 67 views

回答

10

您需要將它們作爲資源包含在項目中,然後通過從DLL中讀取來訪問它們。

GIF文件,你可以簡單地把它們的資源(在項目 - >屬性對話框),然後通過

var img = Properties.Resources.GifName; 

訪問它們對於您可能需要使用嵌入式資源的MP3文件,然後將它們作爲流讀出。爲此,請將項目拖到項目中專用於這些類型文件的文件夾中。右鍵單擊資源管理器中的文件並顯示屬性窗格,並將「構建操作」設置爲「嵌入資源」。

然後,您可以使用類似這樣的代碼(未經測試的vb翻譯,對不起),以流的形式返回。您需要將流轉換爲玩家可以處理的內容。

using System.Linq; // from System.Core. otherwise just translate linq to for-each 
using System.IO; 

public Stream GetStream(string fileName) { 
    // assume we want a resource from the same that called us 
    var ass = Assembly.GetCallingAssembly(); 
    var fullName = GetResourceName(fileName, ass); 
    // ^^ should = MyCorp.FunnyApplication.Mp3Files.<filename>, or similar 
    return ass.GetManifestResourceStream(fullName); 
} 

// looks up a fully qualified resource name from just the file name. this is 
// so you don't have to worry about any namespace issues/folder depth, etc. 
public static string GetResourceName(string fileName, Assembly ass) { 
    var names = ass.GetManifestResourceNames().Where(n => n.EndsWith(fileName)).ToArray(); 
    if (names.Count() > 1) throw new Exception("Multiple matches found."); 
    return names[0]; 
}  

var mp3Stream = GetStream("startup-sound.mp3"); 

var mp3 = new MyMp3Class(mp3stream); // some player-related class that uses the stream 

這裏有一些鏈接,讓你開始

+0

還有一個更完整的 「ResourceHelper」 我使用。只要讓我知道你是否喜歡它(只需通過一個VB-> C#翻譯器) – 2009-04-21 20:07:19

4

它們包含在一個資源文件(RESX)。

1

您應該將它們分配爲資源。如果您在VS中調出屬性面板,請確保將構建操作設置爲資源,並將複製到輸出目錄設置爲不復制。您還可以在資源選項卡中的項目屬性中管理資源。

7

將它們添加爲嵌入式資源,它們將被編譯到dll中。

首先將它們拖放到解決方案資源管理器中,然後將它們添加到項目中,然後轉到它們的屬性以將其構建操作更改爲嵌入式資源。