Stream Writer and Reader

C

csharpula csharp

Hello,
I am using StreamWriter and StreamReader in order to read from a storage
which is being updated once in a while.
How should I implement it? Should I initilize reader and writer each
time? How can I insure that the writing in the file is done from the
last written line an d not overriding the lines which were already read?

THank U!
 
J

Jon Skeet [C# MVP]

csharpula csharp said:
I am using StreamWriter and StreamReader in order to read from a storage
which is being updated once in a while.
How should I implement it? Should I initilize reader and writer each
time?
Probably.

How can I insure that the writing in the file is done from the
last written line an d not overriding the lines which were already read?

Assuming by "storage" you mean "a file" you can either use
File.AppendText or a constructor of StreamWriter which takes a boolean
parameter to indicate overwrite/append behaviour.
 
K

Kevin Spencer

Use a System.IO.FileStream instance to read and write from the file. You can
use the System.File.Open method with one of its' overrides to specify
whether you want to position the FileStream at the beginning or end of the
file. The FileStream class can also seek to different positions in the file.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
C

csharpula csharp

Hello again,
I am doing this code at each update event:

_webRequest = (HttpWebRequest)HttpWebRequest.Create(_monitoredLogUrl);
_webRequest.Proxy.Credentials =
CredentialCache.DefaultNetworkCredentials;

_webResponse = (HttpWebResponse)_webRequest.GetResponse();

_responseStream = _webResponse.GetResponseStream();
_logStreamReader = new StreamReader(_responseStream);

try
{
using (StreamWriter sw = new
StreamWriter(_outputLogFile,true))
{
MoveToUnreadLines(sw);

while ((inputLine = _logStreamReader.ReadLine()) !=
null)
{
sw.WriteLine(inputLine);
_linesRead++;
}
}
}


Somehow when I update the file in the server by adding new rows ,I don't
see them in reader. Why?
 
K

Kevin Spencer

Somehow when I update the file in the server by adding new rows ,I don't
see them in reader. Why?

I don't even understand your question!

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 

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

Top