Write a Manifest Resource to File (VB, .net 2.0)

D

Dinsdale

I must be totally missing something.

I am trying to use DirectX to play an AVI file that is stored as a
Manifest Resource. Apperently the Video class only takes a string path
as a parameter, so I wanted to write the avi to a file from the
manifest. I thought I would just be able to read it in as a Stream and
then write it out to a FileStream but this does not seem to be the
case.

To complicate things, I just found out about the ResourceManager, but
I'm not quite sure how to use that in an abstracted sort of way (the
My.Resource stuff that is generated is very specific and I want it to
be generalized). This is what I have so far:

Function GetVideo(ByVal resourceIdentifier As String, ByVal videoName
As String) As Video

Dim fileLocation As String
Dim videoFileInfo As FileInfo
Dim aviFile As Video

fileLocation = Application.StartupPath + "\" + videoName
videoFileInfo = New FileInfo(fileLocation)

If Not videoFileInfo.Exists Then
Dim resourceStream As Stream
resourceStream = System.Reflection.Assembly _
.GetExecutingAssembly.GetManifestResourceStream(resourceIdentifier
+ "." + videoName)

Dim videoFileStream As New FileStream(fileLocation,
FileMode.Create)

'SO HOW DO I WRITE???

videoFileStream.Close()


End If

aviFile = New Video(fileLocation)

Return aviFile

End Function

Obviously I need some logic to catch any exceptions, but could someone
tell me what the easy way to do this is?

Cheers!
Dinsdale
 
M

Mattias Sjögren

I am trying to use DirectX to play an AVI file that is stored as a
Manifest Resource. Apperently the Video class only takes a string path
as a parameter, so I wanted to write the avi to a file from the
manifest. I thought I would just be able to read it in as a Stream and
then write it out to a FileStream but this does not seem to be the
case.

Can't you just ship it as a separate file?

If not, you simply have to call resourceStream.Read in a loop, reading
a chunk of the stream at a time, and write it to the file with
videoFileStream.Write.


Mattias
 
D

Dinsdale

Can't you just ship it as a separate file?

If not, you simply have to call resourceStream.Read in a loop, reading
a chunk of the stream at a time, and write it to the file with
videoFileStream.Write.

Mattias

Thanks Mattias. I'm curious though; I've seen someone write a byte
array into a file in one statement. Is it not possible to read a
stream into a file in one statement? (disclaimer, I'm a C# programmer
using VB.net so please excuse any syntax errors)

'Read a byte array into a file
Dim byteArray() as Byte = ...

Dim myfile as New FileStream("C:\File.avi", FileMode.Create)

myFile.Write(byteArray, 0, byteArray.Length)

myFile.Close()

Is this not possible with a stream? Can anyone explain why this is?

Cheers
Dinsdale
 
M

Mattias Sjögren

Thanks Mattias. I'm curious though; I've seen someone write a byte
array into a file in one statement. Is it not possible to read a
stream into a file in one statement? (disclaimer, I'm a C# programmer
using VB.net so please excuse any syntax errors)

'Read a byte array into a file
Dim byteArray() as Byte = ...

Dim myfile as New FileStream("C:\File.avi", FileMode.Create)

myFile.Write(byteArray, 0, byteArray.Length)

myFile.Close()
Is this not possible with a stream? Can anyone explain why this is?

Well you can make a single call to Stream.Read and request all of the
data to be read at once (pass in Stream.Length). It will probably work
for most Stream classes (if you stay away from network IO). The
problem with that is that the Stream contract lets implementations
return less than the amount of data requested, and the Length property
may not be supported if the stream isn't seekable. The stream
abstraction is meant to support arbitrary data streams, including
infinite ones.

You can also do sort of what you want with the BinaryReader class.
Like this

Dim br As New BinaryReader(yourStream)
Dim byteArray() as Byte = br.ReadBytes(yourStream.Length)

But again this assumes that the Length property will not throw. And it
doesn't scale well to large amounts of data.

If you know the source and size of your stream you can surely get away
with the above. But when working with arbitrary streams from unknown
sources you have to be a bit more careful.


Mattias
 

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