2009-04-21 71 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中。

首先将它们拖放到解决方案资源管理器中,然后将它们添加到项目中,然后转到它们的属性以将其构建操作更改为嵌入式资源。