using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString());
// multiple sw.WriteLine
}
}
In spite of the fact that FileShare is set to “None”, an Exception is launched “The process cannot access the file because it is being used by another process.
I’m in Multi-thread context and the file is not written/read somewhere else.
Why ?
You are trying to write to a file from multiple threads – this is not possible without some sort of synchronization to ensure the file is closed before attempting to write by another thread.
As for
FileShareset toNone– this means that multiple threads are not allowed to share the file, so of course you get a sharing violation exception.