| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A .NET library providing a virtual file system abstraction.
For current implementations, see Related Projects
The primary interface is IVirtualFileSystem, which exposes methods to:
The VirtualFile class provides properties and methods for creating, deleting, copying, and opening files within the virtual file system.
using Ramstack.FileSystem;
public class Program
{
public static async Task Sample(IVirtualFileSystem fs)
{
var file = fs.GetFile("/sample/hello.txt");
Console.WriteLine($"Writing 'Hello, World!' to '{file.FullName}'");
await using (Stream stream = await file.OpenWriteAsync())
await using (StreamWriter writer = new StreamWriter(stream))
{
await writer.WriteLineAsync("Hello, World!");
}
Console.WriteLine($"Reading contents of '{file.FullName}'");
using (StreamReader reader = await file.OpenTextAsync())
{
Console.WriteLine(await reader.ReadToEndAsync());
}
// Replace content of the file with data from an existing file
await using FileStream input = File.OpenRead(@"D:\sample\hello-world.txt");
await file.WriteAsync(input, overwrite: true);
Console.WriteLine($"The file '{file.Name}' has a length of {await file.GetLengthAsync()} bytes");
Console.WriteLine($"Deleting file '{file.FullName}'");
await file.DeleteAsync();
}
}The VirtualFile class provides the following properties to retrieve information about a file:
The VirtualFile class also exposes a GetPropertiesAsync method to retrieve file properties:
For convenience, many methods specific to VirtualFile are also available as extension methods on IVirtualFileSystem.
For example, if we know the path of the file we want to read, we don't need to obtain a VirtualFile object explicitly:
using StreamReader reader = await fs.OpenTextAsync("/docs/README", Encoding.UTF8);
Console.WriteLine(await reader.ReadToEndAsync());The VirtualDirectory class provides properties and methods for creating, deleting, and enumerating directories and subdirectories.
public static async Task PrintFilesAsync(VirtualDirectory directory, string padding = "", CancellationToken cancellationToken = default)
{
await foreach (VirtualNode node in directory.GetFileNodesAsync(cancellationToken))
{
Console.WriteLine($"{padding}{node.Name}");
if (node is VirtualDirectory dir)
{
await PrintFilesAsync(dir, padding + " ", cancellationToken);
}
}
}
// Print the file tree
await PrintFilesAsync(fs.GetDirectory("/sample"));The VirtualDirectory class provides the following properties to retrieve information about a directory:
The VirtualDirectory class also exposes a GetPropertiesAsync method to retrieve directory properties, if available:
For convenience, many methods specific to VirtualDirectory are also available as extension methods on IVirtualFileSystem.
For example, if we know the path of the desired directory, we don't need to obtain a VirtualDirectory object explicitly:
await foreach (VirtualFile file in fs.GetFilesAsync("/sample/directory"))
{
Console.WriteLine($"Processing: {file.FullName}");
}
// Delete the directory recursively
await fs.DeleteDirectoryAsync("/sample/directory");The file system in use may be read-only, and as a result, any modifying operations on files and directories will throw an exception. To check if the file system is read-only, the IVirtualFileSystem, VirtualFile, and VirtualDirectory classes provide the IsReadOnly property.
if (!fs.IsReadOnly)
{
await fs.DeleteFileAsync("/hello.txt");
}| Version | |
|---|---|
| .NET | 6, 7, 8, 9, 10 |
Bug reports and contributions are welcome.
This package is released as open source under the MIT License. See the LICENSE file for more details.
| Back | FazBrowse Home | New Git URL |