我目前正在研究Be.hex库。 我想将添加到十六进制框中的值保存到新的.bin文件中,而不是保存在先前打开的文件中。 如果我单击“保存文件”按钮。
void save_Click(object sender, EventArgs e)
{
SaveFile();
}
void SaveFile()
{
if (hexBox.ByteProvider == null)
return;
try
{
DynamicFileByteProvider dynamicFileByteProvider = hexBox.ByteProvider as DynamicFileByteProvider;
dynamicFileByteProvider.ApplyChanges();
}
catch (Exception ex1)
{
Program.ShowError(ex1);
}
finally
{
ManageAbility();
}
}
和dynamicFileByteProvider.ApplyChanges();
public void ApplyChanges()
{
if (_readOnly)
throw new OperationCanceledException("File is in read-only mode");
// This method is implemented to efficiently save the changes to the same file stream opened for reading.
// Saving to a separate file would be a much simpler implementation.
// Firstly, extend the file length (if necessary) to ensure that there is enough disk space.
if (_totalLength > _stream.Length)
{
_stream.SetLength(_totalLength);
}
// Secondly, shift around any file sections that have moved.
long dataOffset = 0;
for (DataBlock block = _dataMap.FirstBlock; block != null; block = block.NextBlock)
{
FileDataBlock fileBlock = block as FileDataBlock;
if (fileBlock != null && fileBlock.FileOffset != dataOffset)
{
MoveFileBlock(fileBlock, dataOffset);
}
dataOffset += block.Length;
}
// Next, write in-memory changes.
dataOffset = 0;
for (DataBlock block = _dataMap.FirstBlock; block != null; block = block.NextBlock)
{
MemoryDataBlock memoryBlock = block as MemoryDataBlock;
if (memoryBlock != null)
{
_stream.Position = dataOffset;
for (int memoryOffset = 0; memoryOffset < memoryBlock.Length; memoryOffset += COPY_BLOCK_SIZE)
{
_stream.Write(memoryBlock.Data, memoryOffset, (int)Math.Min(COPY_BLOCK_SIZE, memoryBlock.Length - memoryOffset));
}
}
dataOffset += block.Length;
}
// Finally, if the file has shortened, truncate the stream.
_stream.SetLength(_totalLength);
ReInitialize();
}
因此,从代码中可以看到,它只是将更改应用于之前打开的.bin文件。但是,举例来说,在我将字节数组添加到十六进制框之后
byte[] byteArr = {0xaa, 0x3f, 0x4b};
hexBox.ByteProvider = new DynamicByteProvider(byteArr);
如何创建新的.bin文件并写入从字节数组添加的十六进制值?