I've a requirement like, there a file with name Log.txt
and is a shared file using by two different libraries parent
and child
in parallel. In parent
library we are adding some lines of data after that in child
library deleting the same data in same file. But, we cannot close the FileStream
in parent
because it is keep on adding the data.
Parent code goes like below (Writing the data) :
using (FileStream fileStream = new FileStream(logFilePath, FileMode.Append))
{
using (StreamWriter log = new StreamWriter(fileStream))
{
log.WriteLine(strLog);
}
}
Child code goes like below (Clearing the data):
using (FileStream fileStream = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
//fileStream.SetLength(0);
using (StreamWriter log = new StreamWriter(fileStream))
{
File.WriteAllText(logFilePath, string.Empty);
}
}
Here I'm getting error This file is using by some other process. Yes, I know this error will come for which reason. My question is is there any other way to achieve this flow?
Thanks in advance