Change File Content

  • Thread starter Thread starter Fabian
  • Start date Start date
F

Fabian

Hi

I want to change the content of text file.
I use an StreamReader to read but I can´t use a StreamWriter for the same
Stream.

What I must do to implement content changing?

Thanks for help
Fabian
 
Hi!

Since StreamReader is just for read and StreamWriter just for write try to
use the FileStream instead.

Anders
 
Fabian said:
Hi

I want to change the content of text file.
I use an StreamReader to read but I can´t use a StreamWriter for the same
Stream.
I would assume that you read the file, change the contents, file the changed
contents.
Then what is your problem?
 
Hi,

Writting the file that you are reading is fine ONLY IF you will no change
the size of it and you only write on the part that you read already,
otherwise you could overwrite the original content.
It's much better if you create a temp file, write to it and when done just
delete the original file and rename the temp file.

You can use Path.GetTempPath() to create the new file in
and Guid.NewValue() to name the file with an unique filename.

Something in the back of my head tells me that there is another way to
create a temp file but I dont remember right now.


Cheers,
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Writting the file that you are reading is fine ONLY IF you will no change
the size of it and you only write on the part that you read already,
otherwise you could overwrite the original content.
It's much better if you create a temp file, write to it and when done just
delete the original file and rename the temp file.

You can use Path.GetTempPath() to create the new file in
and Guid.NewValue() to name the file with an unique filename.

Something in the back of my head tells me that there is another way to
create a temp file but I dont remember right now.

Surely:

Create a new_file.
Read old_file.
Wite old_file to new_file.
Substitute what you want to change -- write to new_file.
Carry on writing old_file to new_file.
Blow up old_file.
Rename new_file to old_file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top