I have a number of methods on a class which I am calling via reflection:

```
var instances = new TestsBase[] {
    new CompilerGeneratedTestData(),
    new ConstructedTestData(),
    new VBCompilerGeneratedTestData()
};

var methods = instances
    .SelectMany(x =>
        x.GetType()
            .GetMethods()
            .Select(m => (instance: x, method: m))
    )
    .OrderBy(x => x.method.ReflectedType.Name)
    .ThenBy(x => x.method.Name)
    .ToList();

```

Each of these methods ultimately calls the following code:

```
string[] toWrite = ...
var outfileName = @"c:\path\to\text.txt";
File.AppendAllLines(outfileName, toWrite);

```

When I run the code, at some point I get the following exception at the call to `File.AppendAllLines`:

> 
> **System.IO.IOException**: 'The process cannot access the file 'c:\path\to\text.txt' because it is being used by another process.'
> 
> 
> 

The original method call appears to be different each time; it's not failing on a particular method.

The application is a simple console project, with no multithreading in my code.

**How can I avoid this error?**

---

I have a set of abstract classes with XUnit test methods:

```
public class ConstructedBase {
    protected abstract void RunTest(object o, string csharp, string vb, string factoryMethods);

    [Fact]
    public void ConstructAdd() => RunTest(Expression.Add(x, y), "x + y", "x + y", "Add(x, y)");

    ...
}

```

and a class which defines the implementation of `RunTest`:

```
class ConstructedTestData : ConstructedBase {
    protected override void RunTest(object o, string csharp, string vb, string factoryMethods) => Runner.WriteData(o, factoryMethods);
}

```

as forwarding to a `static` method:

```
public static class Runner {
    public static readonly string outfileName = @"c:\path\to\text.txt";
    public static void WriteData(object o, string testData) {
        string[] toWrite = ...
        File.AppendAllLines(outfileName, toWrite);
    }
}

```

---

**Update**

This error only appears to happen if the text file is simultaneously open in Visual Studio.