public void Logtext(string txt, string fn) { try { StreamWriter sw = File.AppendText(fn); sw.WriteLine(txt); } catch {...} finally { sw.Flush(); sw.Close(); sw.Dispose(); } }
File.AppendText(fn)
The file can not access the file, it is used by another process.
public class WriteIntoFile { private static object m_LogSync = new object(); public static void Logtext(string txt, string fn){ lock(m_LogSync){ using(StreamWriter sw = File.AppendText(fn)){ sw.WriteLine(txt); } } } }
lock
Mutex
FileSystemWatcher
lock (sw) { sw.WriteLine(txt); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)