Search code examples
c#formspathsoundplayer

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

  • 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