Search code examples
c#.netfilerandom-access

How to insert characters to a file using C#


I have a huge file, where I have to insert certain characters at a specific location. What is the easiest way to do that in C# without rewriting the whole file again.


Solution

  • Filesystems do not support "inserting" data in the middle of a file. If you really have a need for a file that can be written to in a sorted kind of way, I suggest you look into using an embedded database.

    You might want to take a look at SQLite or BerkeleyDB.

    Then again, you might be working with a text file or a legacy binary file. In that case your only option is to rewrite the file, at least from the insertion point up to the end.

    I would look at the FileStream class to do random I/O in C#.