https://github.com/testableio/system.io.abstractions.extensions
Convenience functionality on top of System.IO.Abstractions
https://github.com/testableio/system.io.abstractions.extensions
Last synced: 9 months ago
JSON representation
Convenience functionality on top of System.IO.Abstractions
- Host: GitHub
- URL: https://github.com/testableio/system.io.abstractions.extensions
- Owner: TestableIO
- License: mit
- Created: 2021-08-17T09:29:26.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-05-10T23:17:18.000Z (9 months ago)
- Last Synced: 2025-05-11T00:21:38.048Z (9 months ago)
- Language: C#
- Size: 98.6 KB
- Stars: 24
- Watchers: 1
- Forks: 6
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README

[](https://www.nuget.org/packages/TestableIO.System.IO.Abstractions.Extensions)

[](https://renovatebot.com/)
Convenience functionality on top of System.IO.Abstractions
```shell
dotnet add package TestableIO.System.IO.Abstractions.Extensions
```
# Examples
## CurrentDirectory extension
```csharp
var fs = new FileSystem();
//with extension
var current = fs.CurrentDirectory();
//without extension
var current = fs.DirectoryInfo.FromDirectoryName(fs.Directory.GetCurrentDirectory());
```
## SubDirectory extension
```csharp
var current = new FileSystem().CurrentDirectory();
//create a "temp" subdirectory with extension
current.SubDirectory("temp").Create();
//create a "temp" subdirectory without extension
current.FileSystem.DirectoryInfo.FromDirectoryName(current.FileSystem.Path.Combine(current.FullName, "temp")).Create();
```
## File extension
```csharp
var current = new FileSystem().CurrentDirectory();
//create a "test.txt" file with extension
using (var stream = current.File("test.txt").Create())
stream.Dispose();
//create a "test.txt" file without extension
using (var stream = current.FileSystem.FileInfo.FromFileName(current.FileSystem.Path.Combine(current.FullName, "test.txt")).Create())
stream.Dispose();
```
## Automatic cleanup with Disposable extensions
Use `CreateDisposableDirectory` or `CreateDisposableFile` to create a `IDirectoryInfo` or `IFileInfo` that's automatically
deleted when the returned `IDisposable` is disposed.
```csharp
var fs = new FileSystem();
//with extension
using (fs.CreateDisposableDirectory(out IDirectoryInfo dir))
{
Console.WriteLine($"This directory will be deleted when control leaves the using block: '{dir.FullName}'");
}
//without extension
var temp = fs.Path.GetTempPath();
var fileName = fs.Path.GetRandomFileName();
var path = fs.Path.Combine(temp, fileName);
try
{
IDirectoryInfo dir = fs.Directory.CreateDirectory(path);
Console.WriteLine($"This directory will be deleted in the finally block: '{dir.FullName}'");
}
finally
{
fs.Directory.Delete(path, recursive: true);
}
```
## IDirectoryInfo.CopyTo extension
```csharp
var fs = new FileSystem();
var current = fs.CurrentDirectory();
//source
var source = current.SubDirectory("source");
source.Create(); //make sure the source directory exists
//destination
var dest = current.SubDirectory("destination");
//copy
source.CopyTo(dest, recursive: true);
```