您对硬盘驱动器(或任何其他流)的每一个访问,是昂贵的。 将您的代码转换为使用下一个要读取的X
(例如1024)字节和要写入的Y
(例如1024)字节的高速缓存副本。
我没有完全理解你的代码是应该做的,但说你要复制流之间的文件,你的函数应该是在精神:
private const int BUFFER_SIZE = 1024;
void copy(BinaryReader inStream, BinaryWriter outStream)
{
byte[] cache = new byte[BUFFER_SIZE];
int readCount = 0;
while ((readCount = inStream.Read(cache, 0, BUFFER_SIZE)) != 0)
{
outStream.Write(cache, 0, readCount);
}
}
在这个例子中,BUFFER_SIZE
不要太小(所以批量阅读和写作会很有效),而不是太大 - 满溢你的记忆。
在您的代码示例中,您每次读取一个字节(即BUFFER_SIZE = 1
),因此会降低应用程序的速度。
编辑:添加你需要写代码:
public Boolean Patch(string path)
{
const int BUFFER_SIZE = 512;
// VERY IMPORTANT: The throw operation will cause the stream to remain open the function returns.
using (FileStream fs = new FileStream(path, FileMode.Open))
{
BinaryReader br = new BinaryReader(fs);
BinaryWriter bw = new BinaryWriter(fs);
if (fs.Length != this.rawdata.Length)
throw new ArgumentException();
byte[] cache = new byte[BUFFER_SIZE];
int readCount = 0, location = 0;
while ((readCount = br.Read(cache, 0, BUFFER_SIZE)) != 0)
{
int changeLength = 0;
for (int j = 0; j < readCount; j++)
{
if (cache[j] != rawdata[j + location])
{
changeLength++;
}
else if (changeLength > 0)
{
fs.Position = location + j - changeLength;
bw.Write(rawdata, location + j - changeLength, changeLength);
fs.Position = location + j;
changeLength = 0;
}
}
location += readCount;
}
return true;
}
}
这似乎是一个灾难。你不想保存整个文件,因为用户可能正在编辑另一个程序?你处理的唯一情况是如果文件大小不同。如果用户改变了您正在更改的确切代码块中的内容,该怎么办?答:你会覆盖他们的变化。从本质上来说,您的应用程序将保留用户所做的临时更改。如果用户从文件的开头删除3个字节,但在结尾添加3个字节会怎样?然后一切都不同步。 – NotMe
最好的办法是锁定该文件,不要让用户在应用程序关闭之前在应用程序之外进行编辑。 – NotMe