user2569038
user2569038

Reputation: 33

Cannot locate the wav file in the root folder of the project

I have code below. It works fine. But I would like to locate the "sound.wav" file the folder of the project. I mean ı don't want to put it in "D:\audio\background\sound.wav". I put the audio file the folder of the project but I couldn't do that . What changes should I do in System.Uri(@"D:\audio\background\sound.wav"))" Thanks.

my simple program is this. I just want to play sound.wav file from home folder.

private void button1_Click(object sender, EventArgs e)
{
    var background = new System.Windows.Media.MediaPlayer();
    background.Open(new System.Uri(@"D:\sound.wav"));                        
    background.Play();
}

Upvotes: 1

Views: 1243

Answers (3)

terrybozzio
terrybozzio

Reputation: 4542

If you want add your audio files in the resources properties->resources->addresource,after you done that just do this:

    SoundPlayer sndplayr = new SoundPlayer(YourNameSpace.Properties.Resources.TDB_Groove_04_140_BPM__RC_);
    sndplayr.Play();
    //TDB_Groove_04_140_BPM__RC_ was a file i have and added to my project just as example

If you prefer to place files in startup folder then:

var background = new System.Windows.Media.MediaPlayer();
background.Open(new Uri(Application.StartupPath + @"\YourWavFile.wav"));
background.Play();

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You can use Environment.CurrentDirectory to get current working directory of your application. Then use Path.Combine to create path to audio folder inside working directory:

var path = Path.Combine(Environment.CurrentDirectory, "audio", "sound.wav");

Upvotes: 2

Serberuss
Serberuss

Reputation: 2387

Assuming you have a directory called audio in your project you could access the file with a string like this:

"..\audio\sound.wav"

Or if your application is being run in a directory further down in your project you can use "~" to access the home directory

Upvotes: 0

Related Questions