My C# application works perfectly unless trying to access the current date's log file because it is being used by another process but the file can be open through notepad. I asked this question a couple of weeks ago but did not receive an answer that resolved my issue.
private static void WriteFile(string fileToRead, string fileToWrite, string mySearchString, xmlData counter)
{
counter.hitcount = 0;
Stream stream = File.Open(fileToRead, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
{
StreamReader sr = new StreamReader(stream);
while (!sr.EndOfStream)
{
using (var sw = new StreamWriter(fileToWrite, true))
{
if (sw.BaseStream.Position == 0)
{
//write header only once
sw.WriteLine("date time s-sitename s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Cookie) sc-status sc-substatus sc-win32-status sc-bytes time-taken");
}
var count = 1;
while (sr.Peek() != -1)
{
var line = sr.ReadLine();
// skips 4 lines (headers of log file)
if (count > 4)
{
if (line != null && line.Contains(mySearchString))
{
sw.WriteLine(line);
counter.hitcount++;
}
}
count++;
sr.Close();
sw.Close();
}
}
}
}
}
GOAL: I want to read today's log file but it is currently in use. After reading the file, I am extracting certain strings into a new text file. But the file I want to read is being used by another process