0

The error is System.IO.IOException: 'The process cannot access the file 'C:\Users\user\source\repos\WpfApp1911097\bin\Debug\TextWriterOutput.log' because it is being used by another process.'

I write to the log file using:

///Write to external file
System.Diagnostics.Trace.TraceInformation("various data ");
/// You must close or flush the trace to empty the output buffer.
System.Diagnostics.Trace.Flush();

The program to view the log file when a button is clicked is as follows:

private void DisplayExternal_Click(object sender, RoutedEventArgs e)
{
    Stream myStream;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            string strfilename = openFileDialog1.FileName;
            string filetext = File.ReadAllText(strfilename);
            DisplayText.Text = filetext;
        }   
    }
}

Obviously I get the error when I select the log file so I need to disconnect the log file somehow after wrting to it.

Milo
  • 3,365
  • 9
  • 30
  • 44

0 Answers0