Writing a file

V

Victor Rodriguez

Is there a way to write a file from:
Dim datafile As System.IO.Stream =
Me.GetType().Assembly.GetManifestResourceStream("myassembly.filename.dbf")

To a physical file on the hard drive like "C:\filename.dbf"?

thanks in advance...

Victor
 
J

Jon Skeet [C# MVP]

Victor Rodriguez said:
Is there a way to write a file from:
Dim datafile As System.IO.Stream =
Me.GetType().Assembly.GetManifestResourceStream("myassembly.filename.dbf")

To a physical file on the hard drive like "C:\filename.dbf"?

Sure - just open the output stream, and then copy a block at a time
(e.g. 8K) from the manifest resource stream to the output stream until
you've read it all.
 
H

Herfried K. Wagner [MVP]

Victor Rodriguez said:
Is there a way to write a file from:
Dim datafile As System.IO.Stream =
Me.GetType().Assembly.GetManifestResourceStream("myassembly.filename.dbf")

To a physical file on the hard drive like "C:\filename.dbf"?

Written from scratch and thus untested:

\\\
Imports System.IO
Imports System.Reflection
..
..
..
Dim s As Stream = _
[Assembly].GetExecutingAssembly().GetManifestResourceStream( _
"WindowsApplication17.db1.mdb" _
)
Dim Reader As New BinaryReader(s)
Dim fs As New FileStream("C:\db1.mdb", FileMode.CreateNew)
Const BlockSize As Integer = 1024 ' Read blocks of 1,024 bytes.
Dim BytesRead As Long
Dim Buffer() As Byte
Do While BytesRead < Reader.BaseStream.Length
Buffer = Reader.ReadBytes(BlockSize)
fs.Write(Buffer, 0, Buffer.Length)
Debug.Write(System.Text.Encoding.Default.GetString(Buffer))
BytesRead = BytesRead + Buffer.Length
Loop
Reader.Close()
fs.Close()
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top