Hi,
I have a piece of code that needs to be able to modify a few bytes towards the end of a file. The problem is that the files are huge. Up to 100+ Gb.
I need the operation to be as fast as possible but after hours of Googeling, it looks like .Net is rather limited here???
I have mostly been trying using System.IO.FileS tream and know of no other methods. A "reverse" filestream would do but I have know idea how to create one (write from the end instead of the beginning).
Here is sort of what I do:
I have a piece of code that needs to be able to modify a few bytes towards the end of a file. The problem is that the files are huge. Up to 100+ Gb.
I need the operation to be as fast as possible but after hours of Googeling, it looks like .Net is rather limited here???
I have mostly been trying using System.IO.FileS tream and know of no other methods. A "reverse" filestream would do but I have know idea how to create one (write from the end instead of the beginning).
Here is sort of what I do:
Code:
static void Main(string[] args)
{
//Just to simulate a large file
int size = 1000 * 1024 * 1024;
string filename = "blah.dat";
FileStream fs = new FileStream(filename, FileMode.Create);
fs.SetLength(size);
fs.Close();
//Modify the last byte
byte c = 255;
fs = new FileStream(filename, FileMode.Open);
//If I comment this out, the modification happens instantly
fs.Seek(fs.Length - 1, SeekOrigin.Begin);
fs.WriteByte(c);
//Now, since I am modifying the last byte, this last step is extremely slow on large files (as slow as copying the entire file)
fs.Close();
}
Comment