System.IO.Abstractions 14.0.3

Install-Package System.IO.Abstractions -Version 14.0.3
dotnet add package System.IO.Abstractions --version 14.0.3
<PackageReference Include="System.IO.Abstractions" Version="14.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.IO.Abstractions --version 14.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: System.IO.Abstractions, 14.0.3"
#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=14.0.3

// Install System.IO.Abstractions as a Cake Tool
#tool nuget:?package=System.IO.Abstractions&version=14.0.3
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 (189)

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 (41)

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
14.0.3 1,852 11/27/2021
14.0.2 250 11/26/2021
14.0.1 269 11/26/2021
13.2.47 322,427 8/25/2021
13.2.46 315 8/25/2021
13.2.45 204 8/25/2021
13.2.43 160,700 7/27/2021
13.2.42 55,594 7/23/2021
13.2.41 49,694 7/15/2021
13.2.40 1,807 7/14/2021
13.2.39 1,073 7/14/2021
13.2.38 141,256 6/15/2021
13.2.37 5,892 6/10/2021
13.2.36 269 6/10/2021
13.2.35 221 6/10/2021
13.2.34 229 6/10/2021
13.2.33 493,453 5/15/2021
13.2.32 276 5/15/2021
13.2.31 53,429 5/2/2021
13.2.30 395 5/2/2021
13.2.29 146,662 4/14/2021
13.2.28 175,265 3/25/2021
13.2.27 1,201 3/25/2021
13.2.25 239,400 3/9/2021
13.2.24 8,022 3/6/2021
13.2.23 85,629 2/24/2021
13.2.22 411 2/24/2021
13.2.20 5,294 2/23/2021
13.2.19 294 2/23/2021
13.2.18 593 2/23/2021
13.2.17 2,694 2/23/2021
13.2.16 468 2/22/2021
13.2.15 13,941 2/18/2021
13.2.14 365 2/18/2021
13.2.13 364 2/18/2021
13.2.12 299 2/18/2021
13.2.11 13,642 2/16/2021
13.2.10 80,213 2/10/2021
13.2.9 384,486 1/14/2021
13.2.8 81,943 12/22/2020
13.2.7 13,290 12/20/2020
13.2.6 40,056 12/16/2020
13.2.5 55,824 12/9/2020
13.2.4 21,375 12/5/2020
13.2.3 777 12/4/2020
13.2.2 62,868 11/26/2020
13.2.1 8,133 11/26/2020
13.1.2 489 11/25/2020
13.1.1 447 11/25/2020
13.0.1 69,140 11/21/2020
12.2.27 12,862 11/21/2020
12.2.26 2,768 11/20/2020
12.2.25 55,796 11/17/2020
12.2.24 56,971 11/15/2020
12.2.23 496 11/15/2020
12.2.22 619 11/14/2020
12.2.21 5,197 11/12/2020
12.2.20 1,484 11/12/2020
12.2.19 54,927 11/5/2020
12.2.7 171,203 10/15/2020
12.2.6 8,632 10/15/2020
12.2.5 59,839 10/12/2020
12.2.4 433 10/12/2020
12.2.3 39,238 10/12/2020
12.2.2 16,505 10/7/2020
12.2.1 106,127 9/28/2020
12.1.11 1,529 9/28/2020
12.1.10 80,760 9/24/2020
12.1.9 157,286 9/11/2020
12.1.2 539 9/11/2020
12.1.1 424,311 8/3/2020
12.0.13 6,489 8/2/2020
12.0.10 43,985 7/25/2020
12.0.9 36,673 7/21/2020
12.0.8 11,728 7/19/2020
12.0.7 455 7/19/2020
12.0.6 469 7/19/2020
12.0.5 39,956 7/11/2020
12.0.4 41,330 7/2/2020
12.0.3 10,643 6/29/2020
12.0.2 71,406 6/23/2020
12.0.1 43,241 6/20/2020
11.0.18 124,176 6/20/2020
11.0.17 3,994 6/19/2020
11.0.16 638 6/18/2020
11.0.15 6,292 6/18/2020
11.0.14 982 6/17/2020
11.0.13 971 6/17/2020
11.0.12 520 6/17/2020
11.0.11 1,117 6/17/2020
11.0.10 2,000 6/16/2020
11.0.9 391 6/16/2020
11.0.8 474 6/16/2020
11.0.7 272,916 5/28/2020
11.0.6 420,297 5/8/2020
11.0.5 18,998 5/6/2020
11.0.4 41,792 5/1/2020
11.0.3 2,999 4/30/2020
11.0.2 64,764 4/26/2020
11.0.1 532 4/26/2020
10.0.10 23,740 4/20/2020
10.0.9 18,568 4/17/2020
10.0.8 110,872 4/7/2020
10.0.7 9,423 4/3/2020
10.0.6 13,565 4/1/2020
10.0.5 545 4/1/2020
10.0.4 581 4/1/2020
10.0.1 119,352 3/21/2020
9.0.6 31,482 3/19/2020
9.0.5 39,308 3/16/2020
9.0.4 326,456 2/18/2020
9.0.3 1,278 2/18/2020
9.0.2 21,728 2/11/2020
9.0.1 758 2/11/2020
8.1.1 80,340 2/11/2020
8.0.6 690 2/11/2020
8.0.5 95,234 1/29/2020
8.0.4 3,051 1/27/2020
8.0.3 59,568 1/19/2020
7.1.10 68,025 1/17/2020
7.1.8 710 1/17/2020
7.1.4 54,554 1/13/2020
7.1.3 109,780 1/6/2020
7.1.1 241,981 12/21/2019
7.0.16 7,522 12/19/2019
7.0.15 77,124 12/5/2019
7.0.7 533,496 10/21/2019
7.0.5 32,487 10/11/2019
7.0.4 188,310 9/29/2019
6.0.38 58,659 9/26/2019
6.0.36 13,619 9/24/2019
6.0.34 6,077 9/24/2019
6.0.32 67,678 9/9/2019
6.0.27 64,528 9/3/2019
6.0.25 868 9/2/2019
6.0.23 38,779 8/26/2019
6.0.21 81,748 8/12/2019
6.0.19 48,565 8/9/2019
6.0.17 18,201 8/5/2019
6.0.15 129,928 7/9/2019
6.0.14 54,617 6/29/2019
6.0.13 804 6/28/2019
6.0.11 32,596 6/21/2019
6.0.9 716 6/21/2019
6.0.7 25,198 6/16/2019
6.0.6 587 6/16/2019
6.0.5 11,940 6/13/2019
6.0.3 30,868 6/13/2019
6.0.1 96,650 6/7/2019
5.0.1 38,944 6/3/2019
4.2.17 8,941 5/30/2019
4.2.15 16,907 5/28/2019
4.2.13 40,887 5/15/2019
4.2.12 2,252 5/15/2019
4.2.10 54,546 5/10/2019
4.2.9 119,067 5/10/2019
4.2.8 129,948 4/28/2019
4.2.4 44,775 4/19/2019
4.1.6 146,075 4/9/2019
4.0.11 86,015 3/30/2019
3.1.1 139,375 3/10/2019
3.0.10 439,147 1/5/2019
3.0.2 150,843 12/7/2018
2.2.18-beta 966 12/3/2018
2.2.17-beta 598 12/2/2018
2.2.16-beta 574 12/1/2018
2.2.15-beta 562 12/1/2018
2.2.14-beta 565 12/1/2018
2.2.13-beta 597 12/1/2018
2.2.12-beta 606 12/1/2018
2.2.11-beta 582 12/1/2018
2.2.10-beta 552 11/28/2018
2.2.9-beta 827 11/16/2018
2.2.8-beta 10,975 11/9/2018
2.2.7-beta 692 11/5/2018
2.2.6-beta 659 10/30/2018
2.2.5-beta 619 10/30/2018
2.2.4-beta 624 10/30/2018
2.2.3-beta 630 10/29/2018
2.2.2-beta 624 10/25/2018
2.1.0.256 212,915 10/20/2018
2.1.0.247 84,292 10/15/2018
2.1.0.237 15,857 10/14/2018
2.1.0.236 77,282 10/10/2018
2.1.0.235 39,155 10/8/2018
2.1.0.234 833 10/8/2018
2.1.0.233 4,196 10/6/2018
2.1.0.232 4,193 10/4/2018
2.1.0.231 34,996 9/19/2018
2.1.0.230 63,503 9/8/2018
2.1.0.229 2,639 9/6/2018
2.1.0.228 66,788 8/27/2018
2.1.0.227 80,667 8/16/2018
2.1.0.226 10,817 8/14/2018
2.1.0.217 2,680 8/10/2018
2.1.0.216 5,423 8/9/2018
2.1.0.215 5,259 8/6/2018
2.1.0.214 1,233 8/5/2018
2.1.0.213 896 8/4/2018
2.1.0.211 32,051 7/25/2018
2.1.0.210 4,448 7/21/2018
2.1.0.209 48,327 7/20/2018
2.1.0.208 3,814 7/17/2018
2.1.0.207 879 7/17/2018
2.1.0.206 13,542 7/15/2018
2.1.0.205 1,420 7/12/2018
2.1.0.204 1,337 7/11/2018
2.1.0.203 978 7/11/2018
2.1.0.202 8,863 7/10/2018
2.1.0.201 2,492 7/10/2018
2.1.0.200 1,344 7/9/2018
2.1.0.199 998 7/9/2018
2.1.0.198 2,034 7/8/2018
2.1.0.197 995 7/8/2018
2.1.0.196 925 7/8/2018
2.1.0.195 956 7/7/2018
2.1.0.194 967 7/7/2018
2.1.0.193 953 7/7/2018
2.1.0.192 4,749 7/7/2018
2.1.0.191 973 7/7/2018
2.1.0.190 934 7/7/2018
2.1.0.189 1,000 7/7/2018
2.1.0.188 919 7/6/2018
2.1.0.187 12,018 7/6/2018
2.1.0.186 26,857 7/6/2018
2.1.0.185 1,068 7/5/2018
2.1.0.184 9,238 7/4/2018
2.1.0.183 1,000 7/4/2018
2.1.0.182 929 7/4/2018
2.1.0.181 959 7/4/2018
2.1.0.180 972 7/4/2018
2.1.0.179 987 7/4/2018
2.1.0.178 468,195 1/11/2018
2.1.0.177 7,977 1/2/2018
2.1.0.176 43,550 12/8/2017
2.1.0.175 56,199 11/16/2017
2.1.0.174 76,917 11/7/2017
2.1.0.173 1,199 11/7/2017
2.1.0.172 1,156 11/7/2017
2.1.0.171 1,778 11/4/2017
2.1.0.170 1,481 11/4/2017
2.1.0.169 1,169 11/4/2017
2.1.0.168 1,164 11/4/2017
2.1.0.166 1,167 11/4/2017
2.1.0.164 1,146 11/4/2017
2.1.0.163 1,131 11/4/2017
2.1.0.159 40,774 10/22/2017
2.0.0.144 295,033 5/3/2017
2.0.0.143 277,239 4/7/2017
2.0.0.142 855 4/7/2017
2.0.0.141 12,679 3/2/2017
2.0.0.140 23,665 1/17/2017
2.0.0.139 9,551 1/6/2017
2.0.0.138 19,995 11/17/2016
2.0.0.137 24,988 10/14/2016
2.0.0.136 5,885 10/1/2016
2.0.0.124 378,175 2/8/2016
2.0.0.123 19,663 12/29/2015
2.0.0.122 1,192 12/28/2015
2.0.0.121 1,144 12/28/2015
2.0.0.120 7,401 12/6/2015
2.0.0.119 1,156 12/6/2015
2.0.0.118 10,918 11/4/2015
2.0.0.117 6,861 10/19/2015
2.0.0.116 75,651 7/20/2015
2.0.0.115 26,506 5/18/2015
2.0.0.114 1,120 5/18/2015
2.0.0.113 63,367 3/18/2015
2.0.0.112 1,658 3/11/2015
2.0.0.111 966 3/11/2015
2.0.0.110 982 3/11/2015
2.0.0.109 963 3/11/2015
2.0.0.108 2,651 3/4/2015
2.0.0.107 4,374 2/23/2015
2.0.0.106 1,274 2/20/2015
2.0.0.105 1,138 2/19/2015
2.0.0.104 7,401 2/14/2015
2.0.0.103 5,999 2/7/2015
2.0.0.102 1,015 2/7/2015
2.0.0.101 4,386 1/25/2015
2.0.0.100 1,206 1/25/2015
2.0.0.99 1,110 1/25/2015
2.0.0.98 1,669 1/25/2015
1.4.0.93 117,489 1/25/2015
1.4.0.92 54,572 9/29/2014
1.4.0.89 1,030 9/29/2014
1.4.0.88 1,006 9/29/2014
1.4.0.87 13,929 9/21/2014
1.4.0.86 335,462 5/7/2014
1.4.0.85 2,591 4/23/2014
1.4.0.84 2,953 4/7/2014
1.4.0.83 21,391 3/24/2014
1.4.0.82 1,089 3/24/2014
1.4.0.81 5,038 3/17/2014
1.4.0.80 1,107 3/17/2014
1.4.0.79 4,988 3/10/2014
1.4.0.78 1,860 3/2/2014
1.4.0.77 1,143 3/2/2014
1.4.0.76 2,496 2/21/2014
1.4.0.75 1,071 2/20/2014
1.4.0.74 23,373 1/12/2014
1.4.0.73 5,995 12/22/2013
1.4.0.72 4,670 12/1/2013
1.4.0.71 1,115 12/1/2013
1.4.0.70 1,459 11/21/2013
1.4.0.69 1,389 11/20/2013
1.4.0.68 3,317 10/15/2013
1.4.0.67 1,089 10/15/2013
1.4.0.66 19,412 7/31/2013
1.4.0.65 3,036 7/9/2013
1.4.0.64 3,877 4/26/2013
1.4.0.63 1,018 4/26/2013
1.4.0.62 1,091 4/26/2013
1.4.0.61 1,067 4/25/2013
1.4.0.60 992 4/25/2013
1.4.0.59 1,018 4/25/2013
1.4.0.58 1,093 4/25/2013
1.4.0.57 1,036 4/25/2013
1.4.0.56 1,048 4/25/2013
1.4.0.55 1,085 4/25/2013
1.4.0.54 1,054 4/25/2013
1.4.0.53 1,035 4/25/2013
1.4.0.52 1,098 4/25/2013
1.4.0.51 1,077 4/22/2013
1.4.0.50 1,053 4/22/2013
1.4.0.49 2,129 4/11/2013
1.4.0.48 1,784 3/24/2013
1.4.0.47 1,047 3/24/2013
1.4.0.46 1,054 3/24/2013
1.4.0.45 1,099 3/24/2013
1.4.0.44 1,855 3/16/2013
1.4.0.43 1,021 3/16/2013
1.4.0.42 1,070 3/16/2013
1.4.0.41 1,228 3/6/2013
1.4.0.40 3,841 12/24/2012
1.4.0.39 1,040 12/23/2012
1.4.0.38 1,039 12/23/2012
1.4.0.37 2,234 11/29/2012
1.4.0.36 1,120 11/29/2012
1.4.0.35 2,167 9/25/2012
1.4.0.34 1,129 9/25/2012
1.4.0.33 1,135 9/25/2012
1.4.0.32 4,886 7/14/2012
1.4.0.31 1,091 7/14/2012
1.4.0.30 1,076 7/12/2012
1.4.0.29 1,115 7/12/2012
1.4.0.28 1,062 7/12/2012
1.4.0.27 1,065 7/12/2012
1.4.0.26 1,156 7/2/2012
1.4.0.25 1,131 7/2/2012
1.4.0.24 3,309 5/15/2012
1.4.0.23 2,006 4/25/2012
1.4.0.22 1,073 4/25/2012
1.4.0.21 1,066 4/25/2012
1.4.0.20 1,673 4/18/2012
1.4.0.19 1,125 4/18/2012
1.4.0.18 1,082 4/18/2012
1.4.0.17 1,162 4/18/2012
1.4.0.14 1,126 4/4/2012
1.4.0.13 1,498 3/27/2012
1.4.0.12 1,887 9/16/2011
1.4.0.11 1,360 9/16/2011
1.3.0 1,639 5/27/2011
1.2.0 27,826 5/26/2011