I have looked at many posts with the similar title and their suggested answers. But they haven't solved my problem. Examples are this, this and this.
I have a text file that is always locked by a Windows Service (it's a logger basically). What I want to do is to read from this file in my program and also be able to empty its contents.
Do we have a working solution that does not include copying the file?
This is my code for reading the file so far and it of course is not currently working.
serviceLogs = new List<string>();
try
{
//Lock Exclusively the file
using (FileStream fileStream = new FileStream(Globals.serviceLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fileStream))
{
String line;
while ((line = sr.ReadLine()) != null)
serviceLogs.Add(line);
}
}