And*_*ker 11
您需要将它们作为资源包含在项目中,然后通过读取DLL来访问它们.
对于gif文件,您只需将它们放在资源上(在项目 - >属性对话框中),然后通过它们访问它们
var img = Properties.Resources.GifName;
Run Code Online (Sandbox Code Playgroud)
对于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
Run Code Online (Sandbox Code Playgroud)
以下是一些可以帮助您入门的链接
| 归档时间: |
|
| 查看次数: |
1256 次 |
| 最近记录: |