首先在项目的某处创建一个新的资源文件。使用Add -> New element
上下文菜单中的项目资源管理器中做到这一点:(是的,对不起,这是在德国)
一旦你创建你的资源文件,你可以在它的项目资源管理器中双击并添加资源到它。我添加了一个名为out.wav
的.wav
文件。这可能是您的.mp3
文件。 接下来,您只需在代码中访问该流。添加资源文件也会生成一些代码,让您以流的形式访问该文件。用法例如为:
using System;
using System.IO;
using System.Text;
namespace ResourceTest
{
class Program
{
static void Main(string[] args)
{
//Create a memory stream to read the file into..
MemoryStream outStream = new MemoryStream();
//Copy our _out.wav File
SomeResource._out.CopyTo(outStream);
//Also possible:
//SomeResource.ResourceManager.GetStream("out.wav")...
//Read into a byte array.
byte[] file_bytes = outStream.ToArray();
//Close it
outStream.Close();
//Do something with file_bytes..
}
}
}
现在你已经嵌入你的ressource中的ressource文件,它总是访问(即资源总是得到你所编译的二进制复制)
感谢您的帮助。但不幸的是......无法让它工作..(请看附件) –
类名“SomeResource”是从我创建的资源文件的名称('SomeResource.resx')派生而来的。您必须将类名更改为您创建的资源文件的文件名。 (有'Resources.resx',所以你应该试试'Resources._out'?) –
啊哈。就这样。我重新编写了代码,编译时没有错误。但是,然后......它也没有播放任何声音。它看起来像我们设法将资源加载到内存中,但我们如何从内存中播放资源......?对于提出太多问题抱歉。 –