FileMode and FileAccess and IOException: the process cannot access the file 'filename' because it is being used by another process

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); } 
+4
source share
4 answers

You need to make sure that the service and the reader do not open the log file exclusively. Pay attention to line 2 of application A and line 1 of application B

Appendix A:

 if (File.Exists(nFile2)) File.Delete(nFile2); traceFile2 = new FileStream(nFile2, FileMode.Open, FileAccess.Write, 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()); 

and Appendix B:

 using (FileStream fileStream = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var messageAttachment = new Attachment(fileStream, Path.GetFileName(f)); msgMail.Attachments.Add(messageAttachment); } 
+4
source

Of course, you can read and write from / to the same file at the same time (by different threads / processes).

Here is a sample code. Just see how FileStream is created.

 string fname = "a.txt"; //WRITER Task.Factory.StartNew(() => { var f = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); var s = new StreamWriter(f); long l = 0; while (true) { s.WriteLine(l++); s.Flush(); Task.Delay(1000).Wait(); } }); //READER Task.Factory.StartNew(() => { Task.Delay(1000).Wait(); var f = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var s = new StreamReader(f); while (true) { var line = s.ReadLine(); if (line == null) { Task.Delay(100).Wait(); continue; }; Console.WriteLine("> " + line + " <"); } }); 
+2
source

It seems you are not using the Dispose () and Close () methods of the StreamWriter class to release the file.

+1
source

You need to release control of the file from program A. Try closing or removing the stream block when you are done.

Or you can try using , as described in the answer to this question: Release file access

-one
source

Source: https://habr.com/ru/post/1498064/


All Articles