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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ryan AlfordRank #1,411 · 1.2K reputationPosted Feb 26, 2008, 9:48 AM EST
ncfancfa ncvfa3 reputationPosted Feb 27, 2008, 3:49 AM EST
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?
krisatsky2 reputationPosted Feb 29, 2008, 8:17 AM EST
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
_____________