动态加载资源文件 [英] dynamically load resource file
问题描述
我想动态加载一个资源文件.
I want to dynamically load a resource file.
如果我静态地做,显然效果很好:
If i do it statically, it obviously works well:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources._88);
player.Play();
这将加载并播放资源 _88.
This loads and plays the resource _88.
但是,我希望这是动态的,
However, I want this to be dynamically,
我有一个变量num",它可以是 1-90 之间的任何数字,我想加载与该数字相关的资源.
I have a variable 'num' which can be any number from 1-90, and i want to load the resource related to that number.
所以我创建了另一个名为soundURL"的变量,它看起来像:
So I create another variable called 'soundURL' which looks like:
var soundURL = "_" + num;
但是当我将它与前一个一起使用时,它显然不起作用:
But when I use that with the previous it obviously doesnt work:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.soundURL);
我怎样才能克服这个问题?
How can I overcome this?
推荐答案
可以使用ResourceManager按名称加载资源
You can use the ResourceManager to load resources by name
object O = Properties.Resources.ResourceManager.GetObject("_88");
然后你只需要将它作为一个流..
You then just need to cast it as a Stream..
var s = (Stream)O;
System.Media.SoundPlayer player = new System.Media.SoundPlayer(s);
player.Play();
这篇关于动态加载资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!