Copy embedded resource to file

davidh

Active member
Joined
Jul 8, 2003
Messages
31
Location
UK
How can I copy an MP3 file that is embedded in my .exe file out to a file?

Ive managed to copy it to a stream from the resources, but Im confused as to how to write it to the file now. Ive copied it to a stream using the following code,

Dim assm As [Assembly] = [Assembly].GetExecutingAssembly
Dim str As Stream
Dim strResourceName As String = "Project.TRK0" & intTrackNumber & ".mp3"
str = assm.GetManifestResourceStream(strResourceName)

but Im now struggling to copy it out to a file now.

Thanks for any help!
 
after a bit of messing with streams , i embedded an mp3 in to an app and successfully copied it from manifeststream to the hd. heres a quick example of how to.
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            Dim sWriter As Stream = (Me.GetType().Assembly.GetManifestResourceStream("count_down.Big Brovaz - Nu Flow.mp3"))
            Dim x As Integer
            Dim fFile As New FileStream("C:\test.mp3", FileMode.OpenOrCreate)
            For x = 1 To sWriter.Length
                fFile.WriteByte(sWriter.ReadByte)
            Next
            fFile.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
 
A slightly better way (not to mention faster) would be to read larger chunks than one byte at a time. You can get the length of the file in memory via the Length property of the stream. Youll see a big performance increase.
 
How I that ^ Done then please?

and for the other code i got Errors :( y?

Errors:

Type Stream is Not Defined

Error 2:

Type fFile is Not Defined

Please help
 
Imports statement

Assuming youre using VB.NET, you need to have the following line at the top of your source file:

Code:
Imports System.IO

The following declarations would also be useful:

Code:
Option Strict On
Option Explicit On

You can find explanations of these statements on MSDN.

Good luck :cool:
 
Back
Top