https://github.com/tathamoddie/System.IO.Abstractions allows you to do this (example from github)
public class MyComponent
{
readonly IFileSystem fileSystem;
// Create MyComponent with the given fileSystem implementation
public MyComponent(IFileSystem fileSystem)
{
this.fileSystem = fileSystem;
}
/// Create MyComponent
public MyComponent() : this(
fileSystem: new FileSystem() //use default implementation which calls System.IO
)
{
}
public void Validate()
{
foreach (var textFile in fileSystem.Directory.GetFiles(@"c:\", "*.txt", SearchOption.TopDirectoryOnly))
{
var text = fileSystem.File.ReadAllText(textFile);
if (text != "Testing is awesome.")
throw new NotSupportedException("We can't go on together. It's not me, it's you.");
}
}
}