2011-12-07 26 views
0

ファイルに直接書き込む前に、このコードがバイトを一時バッファにコピーしていることが懸念されます。VB.NET - バイトバッファのサブセットをファイルにコミットする適切な方法は何ですか?

メインバッファの末尾から直接ファイルに直接バイトをコピーする方法があるはずです。

'this code copies all bytes starting at integer index from the main buffer into a new file: ga.exe 

'declare temporary buffer 
Dim EXEBytes(bytes.Count - index) As Byte 

'copy subset of bytes over, starting at index 
System.Buffer.BlockCopy(bytes, index, EXEBytes, 0, bytes.Count - index) 

'write bytes from temporary array into file 
File.WriteAllBytes(Server.MapPath("/BIN/ga.exe"), EXEBytes) 

どのようなアイデアですか?

答えて

2

あなたはストリームを使用したほうが良いでしょう、私は信じている:

Public Sub WriteFile(bytes As Byte(), index As Integer) 
    Using oNewStream As IO.FileStream = IO.File.Open(Server.MapPath("/BIN/ga.exe"), IO.FileMode.Create) 
     If oNewStream IsNot Nothing Then 
      oNewStream.Write(bytes, index, bytes.Count - index) 
      oNewStream.Close() 
     End If 
    End Using 
End Sub 
関連する問題