vb.net - Writing to FileStream works, MemoryStream copied to FileStream doesn't -


i have code used filestream, streamwriter , xmldocument produce excel-compatible output files. useful!

however have need make copies of file, , i'd in-memory. took original filestream code , changed filestream memorystream, , wrapped in function:

'---------------------------------------------------------------------------------- friend sub save(optional byval savecalculatedresults boolean = true)     dim mstream memorystream     dim fstream filestream     dim bytes byte()      'make stream containing xml     mstream = toxlsl(savecalculatedresults)     if mstream.length = 0 return      'then read data byte buffer     redim bytes(cint(mstream.length))     mstream.read(bytes, 0, cint(mstream.length))      'and write "us"     fstream = new filestream("c:\outfile.xlsx", filemode.create)     fstream.write(bytes, 0, cint(mstream.length))     fstream.flush() end sub 

this creates file in correct location, has exact same length did before, opening in excel causes error file format being invalid.

can see obvious problems in code? perhaps writing bytes backwards? possibly text encoding problem? 32/64 problem?

p.s. tried using copyto, doesn't seem work in vb?

it requires guessing toxlsl() behavior gives strong hint: memorystream's position located @ end of stream. read() call doesn't read anything. verify checking return value.

just rid of bytes() entirely, wasteful duplicate data this. don't need it, memorystream gives access data:

using fstream = new filestream("c:\outfile.xlsx", filemode.create)     fstream.write(mstream.getbuffer(), 0, cint(mstream.length)) end using 

do note using statement not optional. , cannot write c:\


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -