Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically load resource file

Tags:

c#

resources

wpf

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();

This loads and plays the resource _88.

However, I want this to be dynamically,

I have a variable 'num' which can be any number from 1-90, and i want to load the resource related to that number.

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?

like image 788
RSM Avatar asked Dec 18 '13 16:12

RSM


1 Answers

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();
like image 186
Original10 Avatar answered Oct 23 '22 18:10

Original10