4

I have an application A that generates a text file for tracing.
While, an application B needs read the same text file and attach in a mailmessage.

But I get the following error, when application B try read the text file:

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

Any suggestions ? Maybe better use for FileMode and FileAccess?

Application 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());

Application 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);
                    }
Kiquenet
  • 13,271
  • 31
  • 133
  • 232

4 Answers4

4

You need to make sure that both the service and the reader open the log file non-exclusively. Notice line 2 of App A and Line 1 of App B

Application 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 Application 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);
}
crthompson
  • 14,783
  • 6
  • 51
  • 74
2

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 + " <");
    }
});
I4V
  • 33,572
  • 3
  • 63
  • 78
1

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

NicoRiff
  • 4,629
  • 2
  • 23
  • 53
  • 1
    Not release the file in Application A, because App A is running. Both applications running in the same time. – Kiquenet Aug 20 '13 at 19:58
-1

You need to release control of the file from Program A. Try closing or disposing the streamwriter when you finish.

Or you might attempt using as is described in the answer to this question: Releasing access to files

Community
  • 1
  • 1
jth41
  • 3,542
  • 8
  • 50
  • 105