An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

![System.IO.Abstractions.Extensions](https://socialify.git.ci/System-IO-Abstractions/System.IO.Abstractions.Extensions/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark)
[![NuGet](https://img.shields.io/nuget/v/TestableIO.System.IO.Abstractions.Extensions.svg)](https://www.nuget.org/packages/TestableIO.System.IO.Abstractions.Extensions)
![Continuous Integration](https://github.com/TestableIO/System.IO.Abstractions.Extensions/workflows/Continuous%20Integration/badge.svg)
[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](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);
```