c# - How to write file chunk by chunk in WinRT? -


i trying write buffer data file. receive buffer data in callback function continuously. need read buffer , save in file received. repeated till complete file,i data chunk of 4k in size. below code either throws exception or output file corrupted. please let me know how in winrt.

storagefile file = await windows.storage.applicationdata.current.localfolder.createfileasync(strfilename, windows.storage.creationcollisionoption.replaceexisting); public async void receive(byte[] buffer) {   using (var ostream = await file.openstreamforwriteasync())   {      await ostream.writeasync(buffer, 0, buffer.length);   } } 

the problem in signature of receive. because it's void, not awaited , can run writting processes in same time (that's causes exception, and/or corrupted datas).

i suggest use instead :

storagefile file = await windows.storage.applicationdata.current.localfolder.createfileasync(strfilename, windows.storage.creationcollisionoption.replaceexisting); public async task receive(byte[] buffer) {   using (var ostream = await file.openstreamforwriteasync())   {      await ostream.writeasync(buffer, 0, buffer.length);   } } 

and call

await receive(b); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -