Playing wav file which has relative path inside project

Cristiano picture Cristiano · Apr 17, 2013 · Viewed 12.2k times · Source

I have some problems with relative paths and reproduction of wav files. I have this simple code which works perfectly:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Users\Admin\Documents\Visual Studio 2012\Projects\TestProject\TestProject\Data\Sounds\car.wav";
player.Play();

I want somehow to play that file with relative path but I didn't have success with this:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"Data\Sounds\car.wav";
player.Play();

Thank you!

Answer

George Johnston picture George Johnston · Apr 17, 2013

Is Data directory in the root directory of your application? Are you copying the directory contents as output?

If so, did you mean, Data\Sounds\car.wav?

Which, if running from Visual Studio would be in [projectroot]\[release]\bin\Data\Sounds\car.wav

If you don't see this directory in your bin folder, you'll need to ensure you're selecting all of the files you want copied to your output directory (which will copy the directory structure). You can do this by clicking on the file in your project and selecting the file as output.