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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With