-
-
Notifications
You must be signed in to change notification settings - Fork 10
Simplify.IO
Alexanderius edited this page Jun 27, 2026
·
1 revision
Provides additional IO helper functions for files. Built on top of System.IO.Abstractions, so file operations can be mocked in tests.
Available at NuGet as binary package
All methods are exposed as static members of the FileHelper class.
// Gets or sets the file system used for IO operations. Defaults to a real FileSystem;
// assign a mock IFileSystem in tests. Throws ArgumentNullException if set to null.
public static IFileSystem FileSystem { get; set; }
// Checks whether a file is locked for reading. Throws ArgumentNullException for an empty path
// and FileNotFoundException if the file does not exist.
public static bool IsFileLockedForRead(string filePath);
// Returns the last line of a text file (or null if not found). Throws ArgumentNullException
// for an empty path and FileNotFoundException if the file does not exist.
public static string GetLastLineOfFile(string filePath);
// Replaces invalid file name characters with '_'.
public static string MakeValidFileName(string name);
// Generates the full name of a file in the application base directory by prepending
// AppContext.BaseDirectory to the file name.
public static string GenerateFullName(string fileName);Note:
GenerateFullNameusesAppContext.BaseDirectorywhich, unlike an assembly location, resolves correctly in single-file and AOT deployments whereAssembly.Locationis empty.
// Wait until a file is no longer being written to
while (FileHelper.IsFileLockedForRead(path))
await Task.Delay(100);
var lastLine = FileHelper.GetLastLineOfFile("app.log");
var safeName = FileHelper.MakeValidFileName("report: 2026/06.txt");
var fullPath = FileHelper.GenerateFullName("settings.json");FileHelper.FileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "test.txt", new MockFileData("line 1\nline 2") }
});
var last = FileHelper.GetLastLineOfFile("test.txt"); // "line 2"