I have an application A that generates a text file for tracing.
While application B needs to read the same text file and paste it into mailmessage.
But I get the following error when application B tries to read a text file:
IOException: the process cannot access the file 'filename' because it is being used by another process
Any suggestions? Maybe it's better to use FileMode and FileAccess?
Appendix A
if (File.Exists(nFile2)) File.Delete(nFile2); traceFile2 = File.Open(nFile2, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); if (traceFile2 != null) { var twt2 = new TextWriterTraceListener(traceFile2); // http://www.helixoft.com/blog/archives/20 try { if (twt2.Writer is StreamWriter) { (twt2.Writer as StreamWriter).AutoFlush = true; } } catch { } var indiceTraceFile2 = Trace.Listeners.Add(twt2); System.Diagnostics.Trace.WriteLine("INICIO: " + DateTime.Now.ToString());
Appendix B
using (FileStream fileStream = File.Open(f, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { var messageAttachment = new Attachment(fileStream, Path.GetFileName(f)); msgMail.Attachments.Add(messageAttachment); }
source share