hi
i am trying to write and read a file at the same time.
i tried FileStream but it is not work.
please help! how can do that?
hi
i am trying to write and read a file at the same time.
i tried FileStream but it is not work.
please help! how can do that?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
krisatskyPosted Feb 29, 2008, 1:17 PM
Check if the below code is helpful
_____________________________
FileStream fs = new FileStream(@"C:\Forums\FileReformat\tc.txt", FileMode.Open, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
Console.WriteLine(sr.ReadToEnd());
sw.WriteLine("this is test write line1");
sw.Flush();
sr.BaseStream.Seek(0, SeekOrigin.Begin);
Console.WriteLine(sr.ReadToEnd());
sw.WriteLine("this is test write line2");
sw.Flush();
sr.BaseStream.Seek(0, SeekOrigin.Begin);
Console.WriteLine(sr.ReadToEnd());
Console.Read();
___________________________
and here is the output
__________
InitialText
InitialText
this is test write line1
InitialText
this is test write line1
this is test write line2
_____________
ncfancfa ncvfaPosted Feb 27, 2008, 8:49 AM
but i have to do that
if i open the file read and close then again open for write, lost too much time
in my case i am reading from a port and writing data to file
and same time i have to use that file also for something else.
do you know any method about this?
Ryan AlfordPosted Feb 26, 2008, 2:48 PM