GrumpyMammoth
GrumpyMammoth

Reputation: 135

How to store a file within an application

I'm making a program that will play some music when it starts and that is easy enough. What I want is to have the music file saved as a part of the solution, so that the music will always play, as opposed to pointing the program towards where the music is saved:

Dim wavMusic As String = "\\My Documents\Visual Studio 2010\Song.wav"

So if I were to move the application to a different computer for example, it wouldn't be able to find the song. So I want to have that song as part of the .exe so that it can always find it. Thanks.

Oh and I'm using vb and winforms because I'm doing it at school, where delightful things like C and WPF aren't used.

Upvotes: 0

Views: 1739

Answers (2)

Tarik
Tarik

Reputation: 81801

You can embed an audio file as resource in the resource files:

enter image description here

enter image description here

enter image description here

private void btnDemo2_Click(object sender, EventArgs e)
{
    try
    {
        SoundPlayer sndplayr = new 
            SoundPlayer(PlayWavFiles.Properties.Resources.LoopyMusic);
        if (btnDemo2.Text == "Demo WAV 2")
        {
            sndplayr.PlayLooping();
            btnDemo2.Text = "STOP";
        }
        else
        {
            sndplayr.Stop();
            btnDemo2.Text = "Demo WAV 2";
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + ": " + ex.StackTrace.ToString(), 
            "Error");
    }
}

Please refer to this article: http://www.codeproject.com/Articles/17422/Embedding-and-Playing-WAV-Audio-Files-in-a-WinForm

Upvotes: 0

Ry-
Ry-

Reputation: 225263

Put it inside your project resources, accessible as a tab in your project's properties. You can access all resources using the My.Resources property. So:

  • Double-click on My Project in the Solution Explorer
  • Go to the Resources tab
  • Drag your file into the big white space
  • In your code, access it as My.Resources.Song

Voilà!

Upvotes: 3

Related Questions