My app updates a log .csv file periodically. I read the docs which suggested this approach:
using (StreamWriter sw = File.AppendText(filenameWithPath))
{
sw.WriteLine(someText);
}
Works fine. The problem is the user wants to open the log, when they do the app crashes because the files locked.
I've been trying to work out what to do about that. I want to hold it open so I tried this:
fs =File.Open(filenameWithPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
sr = new StreamWriter(fs);
This creates the file, locks it to others, perfect.. except!!!
sr.WriteLine(data); doesnt write any data! The file is always empty.
What could be going wrong, or what is the right way to do this using the using statement?
Thanks