prepending text to a file

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

Hi

I am writing text to a fileusing the streamwriter writeline method.
When creating the streamwriter it can be opened to append text to a file.
Is there a way, not necessarily with streamwriter, that I can open a file
and prepend text, so that the newly added data appears at the head of the
file?

Thanks

Tony
 
Tony,

I don't think that there is an easy way to do this. What I would do is
create a new temp file and then write your information. Once you have that,
I would open the other file and then write the contents of the file to the
temp file after the information you have written.

Finally, you would delete the old file and then copy the temp file over
to the old file location when done.

If you want this all to happen in the same file, then you might want to
create a memory-mapped file and then perform a memory copy (CopyMemory) to
shift the contents of the file over and then write your data at the
beginning of the file.

Hope this helps.
 
Tony said:
I am writing text to a fileusing the streamwriter writeline method.
When creating the streamwriter it can be opened to append text to a file.
Is there a way, not necessarily with streamwriter, that I can open a file
and prepend text, so that the newly added data appears at the head of the
file?

No. For one thing, I don't know of any mainstream filesystem which
supports this, or at least makes its support widely known.
 
Correct, prepending is a difficult operation to perform... if its text then
stringbuilder can be loaded with the current content, then written back out
to the same file, but obviously if the file size becomes very large it would
get slower and slower and slower.



--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 
Back
Top