'Audio file path not dynamic

I need the audio file to played when the app runs and it does this successfully, the only problem is is when I move the application to a different PC, the sound won't work because the file path doesn't return true any more. How do I fix this?

namespace Dewey_Decimal_System
{
    public partial class Dewery_Decimal_System : Form
    {
        private int _ticks;

        public Dewery_Decimal_System()
        {
            InitializeComponent();
            Second();
            timer1.Start();
            

            SoundPlayer splayer = new SoundPlayer(@"C:\Users\Luke Warren\Desktop\Prog3B\Dewey Decimal 
            System\Media\EntroMusic2.wav");
            splayer.Play();


            panel1.Hide();
            button1.Hide();
            button2.Hide();
            button3.Hide();
            this.Hide();

        }


Solution 1:[1]

You would do something like this to get the path "Data\ich_will.mp3" inside your application environments folder.

string fileName = "ich_will.mp3";
string path = Path.Combine(Environment.CurrentDirectory, @"Data\", fileName);

In my case it would return the following:

C:\MyProjects\Music\MusicApp\bin\Debug\Data\ich_will.mp3

Solution 2:[2]

  • Fastest way: Just use SoundPlayer splayer = new SoundPlayer(@"EntroMusic2.wav"); And move file "EntroMusic2.wav" into the same folder with the application exe file.
  • Ideal way: Move this file path into a config file instead of hard code it.

Solution 3:[3]

I have the same problem, the target pc give error for the audio path is in the home pc! I search google no one have a solution all say the same thing "put it resource, make content. embed in resource, output directory and many answers but nothing worked ! it still error shows file missing which point the file path from the home pc. I was forced to put all audio files in aseperate mapp and send it to the friend and told him to put tha mapp on his pc under C:\GameAudio and changed all the audio in the program to the new location t.ex C:\GameAudio\hit.wav.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ad5001
Solution 2 Dat Nguyen
Solution 3 Ismail Abisman