inserting a new line of text at begin of file

  • Thread starter Thread starter japi
  • Start date Start date
J

japi

Hi,

i would like to know what approach i should use to insert (not append)
a line of text to the begin of an existing text file.

My current approach works, but i am afraid of loosing data if an
exceptions is thrown somewhere in between the process.

My current approach:
1. read current contents of the file
2. create a filestream object (with FileMode.Create) wrapped in a
streamwriter object
3. write the new line of text
4. write the content that was read in step 1

im looking for a more transactional approach.

Thanks in advance.

Cheers,
Jaap
 
Jaap,

You won't be able to get it. Until Vista is released, you won't be able
to incorporate transactional behavior into file operations.

However, I do think you should order your approach somewhat differently.
I would open the new file stream and write the contents, then open the old
stream and copy the contents over. There is no reason to keep the current
files contents in memory while you write the new content (unless you need it
for some other reason).

When Vista comes out, you should be able to use a FileStream in a
TransactionScope and you should be able to perform your file operations as
part of a transaction. If it is not built into the FileStream, you will
always be able to drop down to the API level to perform transactional work.

Hope this helps.
 
Hi Nicholas,

thank you for looking into this problem.

The reason why i keep the content in memory, is because the source and
destination filestreams map to the same physical file. I read the
contents into a string before i close the reading stream. Then i open a
new stream to the same file for writing purposes.

Again, thank you.

Cheers,
Jaap
Jaap,

You won't be able to get it. Until Vista is released, you won't be able
to incorporate transactional behavior into file operations.

However, I do think you should order your approach somewhat differently.
I would open the new file stream and write the contents, then open the old
stream and copy the contents over. There is no reason to keep the current
files contents in memory while you write the new content (unless you need it
for some other reason).

When Vista comes out, you should be able to use a FileStream in a
TransactionScope and you should be able to perform your file operations as
part of a transaction. If it is not built into the FileStream, you will
always be able to drop down to the API level to perform transactional work.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

japi said:
Hi,

i would like to know what approach i should use to insert (not append)
a line of text to the begin of an existing text file.

My current approach works, but i am afraid of loosing data if an
exceptions is thrown somewhere in between the process.

My current approach:
1. read current contents of the file
2. create a filestream object (with FileMode.Create) wrapped in a
streamwriter object
3. write the new line of text
4. write the content that was read in step 1

im looking for a more transactional approach.

Thanks in advance.

Cheers,
Jaap
 
japi said:
The reason why i keep the content in memory, is because the source and
destination filestreams map to the same physical file. I read the
contents into a string before i close the reading stream. Then i open a
new stream to the same file for writing purposes.

using Nicholas's suggestion, you can write to a new "temp" file, then if all
is well, do the delete and rename. I would think there would be more
opportunities for correcting unexpected errors along the way in this
fashion.

-- Alan
 
Back
Top