System.IO.Abstractions 16.1.4

Install-Package System.IO.Abstractions -Version 16.1.4
dotnet add package System.IO.Abstractions --version 16.1.4
<PackageReference Include="System.IO.Abstractions" Version="16.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.IO.Abstractions --version 16.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: System.IO.Abstractions, 16.1.4"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install System.IO.Abstractions as a Cake Addin
#addin nuget:?package=System.IO.Abstractions&version=16.1.4

// Install System.IO.Abstractions as a Cake Tool
#tool nuget:?package=System.IO.Abstractions&version=16.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

System.IO.Abstractions NuGet Continuous Integration Codacy Badge Renovate enabled FOSSA Status

At the core of the library is IFileSystem and FileSystem. Instead of calling methods like File.ReadAllText directly, use IFileSystem.File.ReadAllText. We have exactly the same API, except that ours is injectable and testable.

dotnet add package System.IO.Abstractions
public class MyComponent
{
    readonly IFileSystem fileSystem;

    // <summary>Create MyComponent with the given fileSystem implementation</summary>
    public MyComponent(IFileSystem fileSystem)
    {
        this.fileSystem = fileSystem;
    }
    /// <summary>Create MyComponent</summary>
    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.");
        }
    }
}

The library also ships with a series of test helpers to save you from having to mock out every call, for basic scenarios. They are not a complete copy of a real-life file system, but they'll get you most of the way there.

dotnet add package System.IO.Abstractions.TestingHelpers
[Test]
public void MyComponent_Validate_ShouldThrowNotSupportedExceptionIfTestingIsNotAwesome()
{
    // Arrange
    var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
    {
        { @"c:\myfile.txt", new MockFileData("Testing is meh.") },
        { @"c:\demo\jQuery.js", new MockFileData("some js") },
        { @"c:\demo\image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) }
    });
    var component = new MyComponent(fileSystem);

    try
    {
        // Act
        component.Validate();
    }
    catch (NotSupportedException ex)
    {
        // Assert
        Assert.AreEqual("We can't go on together. It's not me, it's you.", ex.Message);
        return;
    }

    Assert.Fail("The expected exception was not thrown.");
}

We even support casting from the .NET Framework's untestable types to our testable wrappers:

FileInfo SomeApiMethodThatReturnsFileInfo()
{
    return new FileInfo("a");
}

void MyFancyMethod()
{
    var testableFileInfo = (FileInfoBase)SomeApiMethodThatReturnsFileInfo();
    ...
}

Since version 4.0 the top-level APIs expose interfaces instead of abstract base classes (these still exist, though), allowing you to completely mock the file system. Here's a small example, using Moq:

[Test]
public void Test1()
{
    var watcher = Mock.Of<IFileSystemWatcher>();
    var file = Mock.Of<IFile>();

    Mock.Get(file).Setup(f => f.Exists(It.IsAny<string>())).Returns(true);
    Mock.Get(file).Setup(f => f.ReadAllText(It.IsAny<string>())).Throws<OutOfMemoryException>();

    var unitUnderTest = new SomeClassUsingFileSystemWatcher(watcher, file);

    Assert.Throws<OutOfMemoryException>(() => {
        Mock.Get(watcher).Raise(w => w.Created += null, new System.IO.FileSystemEventArgs(System.IO.WatcherChangeTypes.Created, @"C:\Some\Directory", "Some.File"));
    });

    Mock.Get(file).Verify(f => f.Exists(It.IsAny<string>()), Times.Once);

    Assert.True(unitUnderTest.FileWasCreated);
}

public class SomeClassUsingFileSystemWatcher
{
    private readonly IFileSystemWatcher _watcher;
    private readonly IFile _file;

    public bool FileWasCreated { get; private set; }

    public SomeClassUsingFileSystemWatcher(IFileSystemWatcher watcher, IFile file)
    {
        this._file = file;
        this._watcher = watcher;
        this._watcher.Created += Watcher_Created;
    }

    private void Watcher_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        FileWasCreated = true;

        if(_file.Exists(e.FullPath))
        {
            var text = _file.ReadAllText(e.FullPath);
        }
    }
}

NuGet packages (194)

Showing the top 5 NuGet packages that depend on System.IO.Abstractions:

Package Downloads
KubernetesClient

Client library for the Kubernetes open source container orchestrator.

System.IO.Abstractions.TestingHelpers

A set of pre-built mocks to help when testing file system interactions.

VirtoCommerce.Platform.Core

Virto Commerce is a flexible B2B ecommerce solution that offers powerful tools for enterprise business users. https://virtocommerce.com

Reo.Core.Hosting

Package Description

Noggog.CSharpExt

Generic reusable classes and extension methods that apply to no specific project and flavored to taste

GitHub repositories (44)

Showing the top 5 popular GitHub repositories that depend on System.IO.Abstractions:

Repository Stars
microsoft/PowerToys
Windows system utilities to maximize productivity
abpframework/abp
Open Source Web Application Framework for ASP.NET Core
gitextensions/gitextensions
Git Extensions is a standalone UI tool for managing git repositories. It also integrates with Windows Explorer and Microsoft Visual Studio (2015/2017/2019).
JosefNemec/Playnite
Video game library manager with support for wide range of 3rd party libraries and game emulation support, providing one unified interface for your games.
projectkudu/kudu
Kudu is the engine behind git/hg deployments, WebJobs, and various other features in Azure Web Sites. It can also run outside of Azure.
Version Downloads Last updated
16.1.4 6,200 1/12/2022
16.1.2 233 1/12/2022
16.1.1 2,092 1/11/2022
16.0.8 4,402 1/9/2022
16.0.7 336 1/8/2022
16.0.6 98 1/8/2022
16.0.5 66 1/8/2022
16.0.4 73 1/8/2022
16.0.3 3,120 1/6/2022
16.0.2 1,027 1/6/2022
16.0.1 37,261 12/22/2021
15.0.1 957 12/22/2021
14.0.13 27,450 12/11/2021
14.0.12 101 12/11/2021
14.0.11 333 12/11/2021
14.0.10 250 12/11/2021
14.0.9 283 12/11/2021
14.0.8 369 12/11/2021
14.0.7 245 12/10/2021
14.0.6 107 12/10/2021
14.0.5 108 12/10/2021
14.0.4 304 12/10/2021
14.0.3 40,795 11/27/2021
14.0.2 1,952 11/26/2021
14.0.1 1,852 11/26/2021
13.2.47 577,009 8/25/2021
13.2.46 348 8/25/2021
13.2.45 230 8/25/2021
13.2.43 198,221 7/27/2021
13.2.42 120,337 7/23/2021
13.2.41 61,139 7/15/2021
13.2.40 2,068 7/14/2021
13.2.39 1,242 7/14/2021
13.2.38 170,115 6/15/2021
13.2.37 6,283 6/10/2021
13.2.36 295 6/10/2021
13.2.35 248 6/10/2021
13.2.34 257 6/10/2021
13.2.33 715,282 5/15/2021
13.2.32 364 5/15/2021
13.2.31 59,117 5/2/2021
13.2.30 417 5/2/2021
13.2.29 173,073 4/14/2021
13.2.28 216,672 3/25/2021
13.2.27 1,264 3/25/2021
13.2.25 371,932 3/9/2021
13.2.24 9,416 3/6/2021
13.2.23 109,125 2/24/2021
13.2.22 437 2/24/2021
13.2.20 6,365 2/23/2021
13.2.19 316 2/23/2021
13.2.18 633 2/23/2021
13.2.17 2,769 2/23/2021
13.2.16 489 2/22/2021
13.2.15 14,571 2/18/2021
13.2.14 386 2/18/2021
13.2.13 385 2/18/2021
13.2.12 320 2/18/2021
13.2.11 15,123 2/16/2021
13.2.10 93,999 2/10/2021
13.2.9 409,945 1/14/2021
13.2.8 98,464 12/22/2020
13.2.7 13,707 12/20/2020
13.2.6 44,388 12/16/2020
13.2.5 62,756 12/9/2020
13.2.4 24,735 12/5/2020
13.2.3 803 12/4/2020
13.2.2 85,452 11/26/2020
13.2.1 8,445 11/26/2020
13.1.2 511 11/25/2020
13.1.1 467 11/25/2020
13.0.1 79,474 11/21/2020
12.2.27 13,724 11/21/2020
12.2.26 2,869 11/20/2020
12.2.25 60,850 11/17/2020
12.2.24 63,201 11/15/2020
12.2.23 518 11/15/2020
12.2.22 640 11/14/2020
12.2.21 5,394 11/12/2020
12.2.20 1,531 11/12/2020
12.2.19 58,246 11/5/2020
12.2.7 232,809 10/15/2020
12.2.6 9,107 10/15/2020
12.2.5 64,841 10/12/2020
12.2.4 450 10/12/2020
12.2.3 43,182 10/12/2020
12.2.2 18,747 10/7/2020
12.2.1 114,038 9/28/2020
12.1.11 1,594 9/28/2020
12.1.10 81,629 9/24/2020
12.1.9 168,909 9/11/2020
12.1.2 559 9/11/2020
12.1.1 451,233 8/3/2020
12.0.13 7,588 8/2/2020
12.0.10 47,199 7/25/2020
12.0.9 37,457 7/21/2020
12.0.8 12,514 7/19/2020
12.0.7 470 7/19/2020
12.0.6 484 7/19/2020
12.0.5 44,725 7/11/2020
12.0.4 43,458 7/2/2020
12.0.3 11,005 6/29/2020
12.0.2 74,023 6/23/2020
12.0.1 43,551 6/20/2020
11.0.18 283,393 6/20/2020
11.0.17 4,098 6/19/2020
11.0.16 659 6/18/2020
11.0.15 7,065 6/18/2020
11.0.14 1,003 6/17/2020
11.0.13 993 6/17/2020
11.0.12 542 6/17/2020
11.0.11 1,134 6/17/2020
11.0.10 2,154 6/16/2020
11.0.9 406 6/16/2020
11.0.8 496 6/16/2020
11.0.7 374,855 5/28/2020
11.0.6 467,618 5/8/2020
11.0.5 22,569 5/6/2020
11.0.4 43,220 5/1/2020
11.0.3 3,092 4/30/2020
11.0.2 71,224 4/26/2020
11.0.1 555 4/26/2020
10.0.10 24,619 4/20/2020
10.0.9 19,029 4/17/2020
10.0.8 115,813 4/7/2020
10.0.7 9,864 4/3/2020
10.0.6 14,158 4/1/2020
10.0.5 566 4/1/2020
10.0.4 603 4/1/2020
10.0.1 123,590 3/21/2020
9.0.6 31,625 3/19/2020
9.0.5 41,149 3/16/2020
9.0.4 335,178 2/18/2020
9.0.3 1,301 2/18/2020
9.0.2 22,137 2/11/2020
9.0.1 778 2/11/2020
8.1.1 82,368 2/11/2020
8.0.6 714 2/11/2020
8.0.5 96,206 1/29/2020
8.0.4 3,085 1/27/2020
8.0.3 67,837 1/19/2020
7.1.10 75,892 1/17/2020
7.1.8 734 1/17/2020
7.1.4 56,877 1/13/2020
7.1.3 116,619 1/6/2020
7.1.1 260,945 12/21/2019
7.0.16 8,163 12/19/2019
7.0.15 78,622 12/5/2019
7.0.7 554,204 10/21/2019
7.0.5 32,665 10/11/2019
7.0.4 205,509 9/29/2019
6.0.38 63,193 9/26/2019
6.0.36 13,691 9/24/2019
6.0.34 6,112 9/24/2019
6.0.32 68,736 9/9/2019
6.0.27 72,438 9/3/2019
6.0.25 891 9/2/2019
6.0.23 40,415 8/26/2019
6.0.21 83,034 8/12/2019
6.0.19 49,122 8/9/2019
6.0.17 18,756 8/5/2019
6.0.15 132,965 7/9/2019
6.0.14 58,609 6/29/2019
6.0.13 851 6/28/2019
6.0.11 33,147 6/21/2019
6.0.9 740 6/21/2019
6.0.7 25,652 6/16/2019
6.0.6 607 6/16/2019
6.0.5 12,113 6/13/2019
6.0.3 31,157 6/13/2019
6.0.1 99,433 6/7/2019
5.0.1 41,573 6/3/2019
4.2.17 9,459 5/30/2019
4.2.15 17,098 5/28/2019
4.2.13 42,289 5/15/2019
4.2.12 2,385 5/15/2019
4.2.10 55,152 5/10/2019
4.2.9 119,089 5/10/2019
4.2.8 131,619 4/28/2019
4.2.4 45,860 4/19/2019
4.1.6 150,169 4/9/2019
4.0.11 90,499 3/30/2019
3.1.1 144,616 3/10/2019
3.0.10 460,632 1/5/2019
3.0.2 161,806 12/7/2018
2.2.18-beta 1,038 12/3/2018
2.2.17-beta 605 12/2/2018
2.2.16-beta 578 12/1/2018
2.2.15-beta 610 12/1/2018
2.2.14-beta 590 12/1/2018
2.2.13-beta 633 12/1/2018
2.2.12-beta 640 12/1/2018
2.2.11-beta 589 12/1/2018
2.2.10-beta 555 11/28/2018
2.2.9-beta 838 11/16/2018
2.2.8-beta 11,029 11/9/2018
2.2.7-beta 699 11/5/2018
2.2.6-beta 668 10/30/2018
2.2.5-beta 627 10/30/2018
2.2.4-beta 632 10/30/2018
2.2.3-beta 635 10/29/2018
2.2.2-beta 630 10/25/2018
2.1.0.256 218,808 10/20/2018
2.1.0.247 84,779 10/15/2018
2.1.0.237 16,943 10/14/2018
2.1.0.236 78,620 10/10/2018
2.1.0.235 39,279 10/8/2018
2.1.0.234 874 10/8/2018
2.1.0.233 4,949 10/6/2018
2.1.0.232 4,382 10/4/2018
2.1.0.231 36,187 9/19/2018
2.1.0.230 65,335 9/8/2018
2.1.0.229 2,698 9/6/2018
2.1.0.228 75,176 8/27/2018
2.1.0.227 85,728 8/16/2018
2.1.0.226 10,929 8/14/2018
2.1.0.217 2,711 8/10/2018
2.1.0.216 5,463 8/9/2018
2.1.0.215 5,425 8/6/2018
2.1.0.214 1,276 8/5/2018
2.1.0.213 936 8/4/2018
2.1.0.211 32,131 7/25/2018
2.1.0.210 4,510 7/21/2018
2.1.0.209 52,127 7/20/2018
2.1.0.208 3,879 7/17/2018
2.1.0.207 929 7/17/2018
2.1.0.206 15,496 7/15/2018
2.1.0.205 1,498 7/12/2018
2.1.0.204 1,379 7/11/2018
2.1.0.203 1,066 7/11/2018
2.1.0.202 8,923 7/10/2018
2.1.0.201 2,611 7/10/2018
2.1.0.200 1,398 7/9/2018
2.1.0.199 1,048 7/9/2018
2.1.0.198 2,090 7/8/2018
2.1.0.197 1,042 7/8/2018
2.1.0.196 967 7/8/2018
2.1.0.195 1,002 7/7/2018
2.1.0.194 1,023 7/7/2018
2.1.0.193 1,006 7/7/2018
2.1.0.192 5,101 7/7/2018
2.1.0.191 1,015 7/7/2018
2.1.0.190 985 7/7/2018
2.1.0.189 1,044 7/7/2018
2.1.0.188 964 7/6/2018
2.1.0.187 12,269 7/6/2018
2.1.0.186 29,850 7/6/2018
2.1.0.185 1,118 7/5/2018
2.1.0.184 9,300 7/4/2018
2.1.0.183 1,046 7/4/2018
2.1.0.182 976 7/4/2018
2.1.0.181 1,009 7/4/2018
2.1.0.180 1,025 7/4/2018
2.1.0.179 1,040 7/4/2018
2.1.0.178 480,602 1/11/2018
2.1.0.177 8,321 1/2/2018
2.1.0.176 46,592 12/8/2017
2.1.0.175 58,306 11/16/2017
2.1.0.174 79,686 11/7/2017
2.1.0.173 1,251 11/7/2017
2.1.0.172 1,201 11/7/2017
2.1.0.171 1,825 11/4/2017
2.1.0.170 1,530 11/4/2017
2.1.0.169 1,214 11/4/2017
2.1.0.168 1,211 11/4/2017
2.1.0.166 1,218 11/4/2017
2.1.0.164 1,194 11/4/2017
2.1.0.163 1,178 11/4/2017
2.1.0.159 41,368 10/22/2017
2.0.0.144 300,254 5/3/2017
2.0.0.143 278,135 4/7/2017
2.0.0.142 900 4/7/2017
2.0.0.141 12,985 3/2/2017
2.0.0.140 24,173 1/17/2017
2.0.0.139 9,598 1/6/2017
2.0.0.138 20,807 11/17/2016
2.0.0.137 25,359 10/14/2016
2.0.0.136 5,994 10/1/2016
2.0.0.124 391,898 2/8/2016
2.0.0.123 20,018 12/29/2015
2.0.0.122 1,242 12/28/2015
2.0.0.121 1,192 12/28/2015
2.0.0.120 7,479 12/6/2015
2.0.0.119 1,237 12/6/2015
2.0.0.118 11,324 11/4/2015
2.0.0.117 6,920 10/19/2015
2.0.0.116 76,895 7/20/2015
2.0.0.115 26,670 5/18/2015
2.0.0.114 1,188 5/18/2015
2.0.0.113 63,925 3/18/2015
2.0.0.112 1,703 3/11/2015
2.0.0.111 1,011 3/11/2015
2.0.0.110 1,026 3/11/2015
2.0.0.109 1,005 3/11/2015
2.0.0.108 2,719 3/4/2015
2.0.0.107 4,818 2/23/2015
2.0.0.106 1,592 2/20/2015
2.0.0.105 1,460 2/19/2015
2.0.0.104 7,590 2/14/2015
2.0.0.103 6,058 2/7/2015
2.0.0.102 1,072 2/7/2015
2.0.0.101 4,444 1/25/2015
2.0.0.100 1,251 1/25/2015
2.0.0.99 1,157 1/25/2015
2.0.0.98 1,721 1/25/2015
1.4.0.93 121,203 1/25/2015
1.4.0.92 55,197 9/29/2014
1.4.0.89 1,067 9/29/2014
1.4.0.88 1,046 9/29/2014
1.4.0.87 13,994 9/21/2014
1.4.0.86 340,576 5/7/2014
1.4.0.85 2,656 4/23/2014
1.4.0.84 3,020 4/7/2014
1.4.0.83 21,639 3/24/2014
1.4.0.82 1,133 3/24/2014
1.4.0.81 5,235 3/17/2014
1.4.0.80 1,152 3/17/2014
1.4.0.79 5,125 3/10/2014
1.4.0.78 1,925 3/2/2014
1.4.0.77 1,185 3/2/2014
1.4.0.76 2,588 2/21/2014
1.4.0.75 1,108 2/20/2014
1.4.0.74 23,873 1/12/2014
1.4.0.73 6,271 12/22/2013
1.4.0.72 4,754 12/1/2013
1.4.0.71 1,153 12/1/2013
1.4.0.70 1,498 11/21/2013
1.4.0.69 1,430 11/20/2013
1.4.0.68 3,356 10/15/2013
1.4.0.67 1,124 10/15/2013
1.4.0.66 20,046 7/31/2013
1.4.0.65 3,084 7/9/2013
1.4.0.64 3,962 4/26/2013
1.4.0.63 1,054 4/26/2013
1.4.0.62 1,129 4/26/2013
1.4.0.61 1,104 4/25/2013
1.4.0.60 1,033 4/25/2013
1.4.0.59 1,053 4/25/2013
1.4.0.58 1,131 4/25/2013
1.4.0.57 1,074 4/25/2013
1.4.0.56 1,086 4/25/2013
1.4.0.55 1,121 4/25/2013
1.4.0.54 1,093 4/25/2013
1.4.0.53 1,074 4/25/2013
1.4.0.52 1,135 4/25/2013
1.4.0.51 1,116 4/22/2013
1.4.0.50 1,092 4/22/2013
1.4.0.49 2,167 4/11/2013
1.4.0.48 1,864 3/24/2013
1.4.0.47 1,085 3/24/2013
1.4.0.46 1,093 3/24/2013
1.4.0.45 1,134 3/24/2013
1.4.0.44 1,897 3/16/2013
1.4.0.43 1,063 3/16/2013
1.4.0.42 1,109 3/16/2013
1.4.0.41 1,266 3/6/2013
1.4.0.40 3,883 12/24/2012
1.4.0.39 1,080 12/23/2012
1.4.0.38 1,079 12/23/2012
1.4.0.37 2,276 11/29/2012
1.4.0.36 1,159 11/29/2012
1.4.0.35 2,219 9/25/2012
1.4.0.34 1,170 9/25/2012
1.4.0.33 1,174 9/25/2012
1.4.0.32 5,010 7/14/2012
1.4.0.31 1,134 7/14/2012
1.4.0.30 1,115 7/12/2012
1.4.0.29 1,155 7/12/2012
1.4.0.28 1,104 7/12/2012
1.4.0.27 1,107 7/12/2012
1.4.0.26 1,194 7/2/2012
1.4.0.25 1,172 7/2/2012
1.4.0.24 3,350 5/15/2012
1.4.0.23 2,071 4/25/2012
1.4.0.22 1,118 4/25/2012
1.4.0.21 1,106 4/25/2012
1.4.0.20 1,715 4/18/2012
1.4.0.19 1,168 4/18/2012
1.4.0.18 1,126 4/18/2012
1.4.0.17 1,205 4/18/2012
1.4.0.14 1,170 4/4/2012
1.4.0.13 1,537 3/27/2012
1.4.0.12 1,929 9/16/2011
1.4.0.11 1,402 9/16/2011
1.3.0 1,684 5/27/2011
1.2.0 28,434 5/26/2011