我想将字节数组附加到现有文件。它必须在文件的末尾。我已经可以设法在文件的开头写了。(感谢stackoverflow ;))。
代码:
public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
try
{
// Open file for reading
System.IO.FileStream _FileStream =
new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
// Writes a block of bytes to this stream using data from
// a byte array.
_FileStream.Write(_ByteArray, 0, _ByteArray.Length);
// close file stream
_FileStream.Close();
return true;
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}",
_Exception.ToString());
}
// error occured, return false
return false;
}
从这里得到它:
但我需要在文件末尾
提前致谢。
找到了解决方案:
FileStream writeStream;
try
{
writeStream = new FileStream(_FileName, FileMode.Append,FileAccess.Write);
BinaryWriter writeBinay = new BinaryWriter(writeStream);
writeBinay.Write(_ByteArray);
writeBinay.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}