Playing wav file which has relative path inside project

12,519

Solution 1

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.

Solution 2

Get the full path of your file with Path.GetFullPath("relativ/path")

Solution 3

You might be better off using absolute path after all. You can get the root path from the exe file, then append your relative path to it. Like this:

// getting root path
string rootLocation = typeof(Program).Assembly.Location;
// appending sound location
string fullPathToSound = Path.Combine(rootLocation, @"Data\Sounds\car.wav");
player.SoundLocation = fullPathToSound;
Share:
12,519
Cristiano
Author by

Cristiano

Updated on June 04, 2022

Comments

  • Cristiano
    Cristiano about 2 years

    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!