C# 将文件从地址字节复制到地址字节
我在四处寻找一种方法,将文件的一部分从一个确定的地址复制到另一个地址,有没有一种方法可以在C#中实现 例如,假设我有这样一个文件:C# 将文件从地址字节复制到地址字节,c#,byte,memory-address,C#,Byte,Memory Address,我在四处寻找一种方法,将文件的一部分从一个确定的地址复制到另一个地址,有没有一种方法可以在C#中实现 例如,假设我有这样一个文件: 我想从0xA0复制到0xB0,然后将其粘贴到另一个文件。它应该类似于: // input data string inputName = "input.bin"; long startInput = 0xa0; long endInput = 0xb0; // excluding 0xb0 that is not copied string outputName
我想从0xA0复制到0xB0,然后将其粘贴到另一个文件。它应该类似于:
// input data
string inputName = "input.bin";
long startInput = 0xa0;
long endInput = 0xb0; // excluding 0xb0 that is not copied
string outputName = "output.bin";
long startOutput = 0xa0;
// begin of code
long count = endInput - startInput;
using (var fs = File.OpenRead(inputName))
using (var fs2 = File.OpenWrite(outputName))
{
fs.Seek(startInput, SeekOrigin.Begin);
fs2.Seek(startOutput, SeekOrigin.Begin);
byte[] buf = new byte[4096];
while (count > 0)
{
int read = fs.Read(buf, 0, (int)Math.Min(buf.Length, count));
if (read == 0)
{
// end of file encountered
throw new IOException("end of file encountered");
}
fs2.Write(buf, 0, read);
count -= read;
}
}
也许是这样的:
long start = 0xA0;
int length = 0xB0 - start;
byte[] data = new byte[length];
using (FileStream fs = File.OpenRead(@"C:\Temp\InputFile.txt"))
{
fs.Seek(start, SeekOrigin.Begin);
fs.Read(data, 0, length);
}
File.WriteAllBytes(@"C:\Temp\OutputFile.txt", data);
抛出新异常()代码>-有些东西(我永远不会告诉你是什么!)出错了-如果你抛出异常,请具体说明:至少抛出新IOException(“遇到文件结尾”)代码>@DmitryBychenko我在异常抛出方面的懒惰是一个传奇:-)