Question : VB.NET 3.5: how to copy an embedded resource to a file

I've been struggling with what I thought would be fairly simple, and that is to copy an embedded binary file to a location on the hard disk.  Attached are two solutions I've tried, but they both error out on "NullReferenceException was unhandled".

Googling returns some examples, even from EE, but none work or they aren't quite what I'm looking for.  I'm not good at reading bytes and trying to write them somewhere -- I usually get my types mixed up and cant seem to get data from one place to another.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
 ''' FIRST ATTEMPT
 Dim sWriter As Stream = (Me.GetType().Assembly.GetManifestResourceStream("foo"))
 Dim x As Integer
 Dim fFile As New FileStream("bar.ext", FileMode.OpenOrCreate)
 For x = 1 To sWriter.Length
     fFile.WriteByte(sWriter.ReadByte)
 Next
 fFile.Close()
 
 
 
 ''' SECOND ATTEMPT
 'Open the Embeded resource
 Dim resourceStream As IO.Stream = Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Foo")
 
 'Open FileStream to write out to
 Dim writer As New IO.FileStream("bar.ext", IO.FileMode.Create, IO.FileAccess.Write)
 
 
 'Write out all bytes of the stream
 Const size As Int16 = 4096
 Dim buffer(size) As Byte
 Dim numBytes As Int32 = 0
 
 Do
     numBytes = resourceStream.Read(buffer, 0, size)
 
     writer.Write(buffer, 0, numBytes)
 
 Loop While (numBytes > 0)
 
 
 'Close both streams and clean up
 resourceStream.Close()
 resourceStream.Dispose()
 
 writer.Close()
 writer.Dispose()

Answer : VB.NET 3.5: how to copy an embedded resource to a file

Yeah, for some reason that read statement doesn't work good in that dowhile loop in vb.net, works fine in c#, change to this:

Dim read As Integer = s.Read(buffer, 0, buffersize)
Do While (read > 0)
            fs.Write(buffer, 0, read)
            read = s.Read(buffer, 0, buffersize)
Loop
Random Solutions  
 
programming4us programming4us