How to Play Embedded Resource Wav Sound in VB.NET 2005, VB 2008, and Visual Basic 2010

I’ve recenty been making an example program with Visual Basic.NET 2008 on snapping forms to the edge of the screen. I wanted the program to play a sound wave file when it performed the snapping process. But I didn’t want to have to make it link to external wave files or anything. So I decided to embed the wave sound bytes(bites?) in the program itself. So this little article will show you how I went about doing that. Here is a link to the actual example application that this article is based on.
More Info

The first thing you want to do of course is determine the wave files you want to play in your application. Remember that the size of each .wav file in bytes, willl be added to your programs file size. So remember that when you decide on what sound files you want to use.

I also want to mention that this article is specific to VB .NET 2005 and higher since I am using the SoundPlayer class. That class was not added until VB 2005. Visual Basic .NET 2002 and VB.NET 2003 will have to use the PlaySound api thats part of the WinMM.dll library. The PlaySound api can play and do everything the SoundPlayer class in VB 2005 and higher can do. Just more code is involved. I actually found an article after I started this post that shows how to play wave files embedded in your VB.NET 02/03 application. Just click here to check it out.
Embedding your .Wave Files

After you have determined the wave files you want to embed in your program you need to add them to your project. There are a couple ways to do this. The easiest way is to copy your .wav file and then paste it in your project. To paste it to your project you just need to select your project name in the Solution Explorer, usually the top right panel. Then right click and click on the paste command. Another way is to click on the “Project” and click on “Add Existing Item”. Then just browse to the wave files location and select each file you want to embed. Once the files are added to your project, you will want to click on each wave file and in the properties panel (Its right below the Solution Explorer by Default). In the Properties panel you will see “Build Action”. From the build action combo list select “Embedded Resource”. Do that for each sound file you added to your project.

Playing your Embedded Files

Now that you have embedded your files into your application, its time to setup the code to play them. If your using .NET older than 2005 then check out this article on using the unManaged api call “PlaySound” to do the playback.

You now need to access your newly embedded *.wav file. You can access it as a stream under your programs manifest. Here is the method that will be used…
Public Overridable Function GetManifestResourceStream(ByVal name As String) As System.IO.Stream
You access the resource manifest through the: Reflection.Assembly.GetExecutingAssembly namespace. You have to provide the name of your programs assembly and the name of your wave file. The name of my example project is: ‘playEmbeddedWaveFileEx’ and the name of my .wav file is: ‘waveFile.wav’. Remember that your assembly and wav file IS CASE Sensitive. In other words, each letter has to be the exact upper casing and lower casing that your actual assembly and wave file is. So for my example, you would pass the assembly and wave file as: ‘playEmbeddedWaveFileEx.waveFile.wav’. Notice the . or period seperating the assembly name and the name of the wave file. An easy way of getting your assembly name is using this Property: My.Application.Info.AssemblyName. First is the code for literally specifying the name of your assembly. So you pass it to the GetManifestResourceStream’s parameter like below…
BUT a better way…
Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(My.Application.Info.AssemblyName & “.waveFile.wav”)

Now all you have to do is create a new SoundPlayer or access the Soundplayer under the ‘MY’ interface and pass the stream and simply start playing the wavefile like below…

        'This creates a new instance of the SoundPlayer class while passing the stream the wave file
        'is embedded in.
        Dim audioPlayer As New Media.SoundPlayer(Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream( _
            My.Application.Info.AssemblyName & ".waveFile.wav")) ' "playEmbeddedWaveFileEx.waveFile.wav"))


OR you can use the MY Interface…

        'This code uses MY, which basically makes it a touch easier since you want have to create
        'a instance of the SoundPlayer class.
        My.Computer.Audio.Play((Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream( _
            My.Application.Info.AssemblyName & ".waveFile.wav")), AudioPlayMode.Background)

Remember that I have a example program on how to perform the steps in this article. Click here to download.

OK, so playing embedded wave files isn’t as straight forward perhaps as it should be. I think I found out about using the GetManifestResourceStream at If you know of something better definitely leave a comment. Either way, the code I provided here DOES work. At least for me. :) Have fun!


Check if a File or Directory Exists using Visual Basic 6.0

  There could be a time when you need to check if files or folders/directories exists or not. There are actually a few different ways to accomplish this task. Some ways is by messing around with the Dir Function located in the FileSystem Class, the Windows API or using the FileSystemObject located in the Windows Scripting Host Object Model. I will show how to use the Dir method and the FileSystem Scripting Object to check if a folder/file exits or not.


Using the DIR Function…

  This feature is built into the core of the Visual Basic 6.0 Runtime, so you don’t need to add any references or components. In my experience this method works just fine, but there could be a scenerio that it may not work for you. I haven’t run into one yet though.


  Basically what you want to do it call the Dir function while putting the path to the file or directory and if it returns nothing then its not seeing the path you specified and thus the file or directory does not exists.


    If Dir("c:\myFile.txt") <> "" Then
        MsgBox "It Exists!"
        MsgBox "No Go!"
    End If


Thats all there is to it! As I said, I do not know how this method would work in every scenerio, but i’ve seen no problems yet.


Using the Scripting FileSystemObject


  This method is not built into the VB 5.0 or 6.0 runtime. So you will first want to go to the Project menu and click on the References item. Once all of the objects are displayed scroll down till you see – “Windows Script Host Object Model”, check it then click OK.

  You now need to create a reference to the FileSystemObject in the scripting class you just added…

    Dim f As FileSystemObject
    Set f = New FileSystemObject


Now you just need to call the available FileExists and FolderExists Functions while passing the path for the file and the path for the directory you want to check.

    MsgBox f.FileExists("c:\myFile.txt")
    MsgBox f.FolderExists("c:\")

Depending on whether the file or folder path you specified exits or not the messagebox should have thrown a True or False message.


  Thats all there is to it for doing basic checking to see if a file exists or if a folder/directory exists or not. The FileSystemObject method is more elegant and probably more reliable than using the Dir Function but at the cost of having to add a Reference to the Windows Scripting Object which is not apart of the Visual Basic 5.0 or Visual Basic 6.0 Runtime Library. The Dir method still seems to work ok for me. Also both ways appear to be case in-sensitive so you won’t have to worry about the letter casing being exact. IF you know of some other ways please feel free to leave a message with the way you do it. Anyways, Have Fun!



Extract/Return the file Path and Filename without the Extension using VB.NET


This will take a file path, extract and return the filesystem path along with the filename but WITHOUT the extension.

        Dim filePath As String = "c:\MyDirectory\MYFile.txt"

        Dim extensionPosition As Integer = filePath.LastIndexOf(".")

        Dim filePathNoEx As String = filePath.Substring(0, extensionPosition)



Get or Extract/Trim the extension from a filename and path in VB.NET

This will remove the extension from the filename and path and return the extension only. There are two methods of doing it below. If you don’t care if the . (period) is included with the extension then I recommend the second way. This works for all versions of .NET including the original VB.NET’s and the latest Visual Basic 2010 version.


Here is One way to do it…


'This will extract and return the extension of a specified filename/path.
Dim filePath As String = "c:\MyDirectory\MYFile.txt"
Dim dotPosition As Integer = filePath.LastIndexOf(".")
Dim filenameOnly As String = filePath.Substring(dotPosition + 1)




Another easy Way from Jim. But it Will include the period with the extension string.


'Thanks to Jim for this other method to get the file extension. Just remember that the "."
'(dot or period) will be included with the extension. So you will need to add codes to remove
'it if you don't want the extension seperator character included.

Dim fullPath As String = "c:\MyDirectory\MYFile.txt"
Dim fileName As String = IO.Path.GetExtension(fullPath)



How to Delete a File using VB 6.0

This will completely delete a file from the hard drive using Visual Basic 6.0. This will permanently delete the file! It will NOT send it to the recycle bin.

        Dim fileToDelete As String

        fileToDelete = "c:\somefile.file"


Check if a Specified File or Directory Exists in VB 2005/2008/2010

Shows how to use VB 2005 and Higher to check if a file and if a folder or directory exists or not.


        'Visual Basic.NET 2005, VB 2008, and VB 2010 Only!
        'Check whether a File or Directory/Folder exists using MY with Visual Basic .NET 2005 or .NET 2008.



Read a Text File and Add text to a Textbox Control With Stream Reader Class – VB.NET

This will open a textfile and then add the text contents to a textbox control using VB.NET 2002/2003, and Visual Basic 2005, VB 2008, and VB 2010.


        'Read from a text file to a textbox with the Stream Reader Class
        'Put a textbox on the form and name txt and set to multi-line
        'This will make the Reader read the entire contents of the textfile and write to the 
        'Create a new stream reader specifying its source to be from the newTextFile.txt file.
        Dim sReader As IO.StreamReader = New IO.StreamReader("c:\newTextFile.txt")
        'Make the textbox keep the current data while adding the new data from the stream.

How to Check if a File, and if a Directory or Folder Exists using VB.NET and Higher

This shows how to check if a File or a Directory and Folder Exists using VB.NET and Higher


This will check if a File exists or not



This will check if a Folder or Directory exists or not