IFileSystem
IFileSystem is an abstraction to access filesystem functionality.
string path = AppDomain.CurrentDomain.BaseDirectory;
IFileSystem filesystem = new FileSystem(path);
IFileSystem.Browse() browses for files and directories in a path.
foreach (var entry in filesystem.Browse(""))
Console.WriteLine(entry.Path);
IFileSystem.Exists() tests whether file or directory exists in a path.
bool exists = filesystem.Exists("dir/");
IFileSystem.OpenStream() opens stream.
IFileSystem filesystem = new FileSystem(AppDomain.CurrentDomain.BaseDirectory);
using Stream s = filesystem.OpenStream("file.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
Console.WriteLine(s.Length);
IFileSystem.OpenMemory() opens memory.
IFileSystem filesystem = new FileSystem(AppDomain.CurrentDomain.BaseDirectory);
using IMemory<byte> s = filesystem.OpenMemory("somefile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
s.Add(item: 32);
IFileSystem.Observe() can observe directories and files for modifications.
IFileSystem filesystem = new FileSystem(AppDomain.CurrentDomain.BaseDirectory);
IObserver<IEvent> observer = new Observer();
using (IDisposable handle = filesystem.Observe("**", observer))
{
}
IFileSystem.Move() can move and rename files and directories.
IFileSystem filesystem = new FileSystem(AppDomain.CurrentDomain.BaseDirectory);
filesystem.CreateDirectory("dir/");
IFileSystem.CreateDirectory() makes a directory.
filesystem.Move("dir/", "new-name/");
IFileSystem.Delete() deletes a file.
filesystem.Delete("file.txt");
And a directory.
filesystem.Delete("dir/", recurse: true);
IFileSystem is very simple to implement.
class ExampleFileSystem : IFileSystem, IFileSystemBrowse, IFileSystemOpenStream, IFileSystemOpenMemory, IPathInfo
{
public bool CanOpen => true;
public bool CanRead => true;
public bool CanWrite => true;
public bool CanCreateFile => true;
public bool CanBrowse => true;
public bool CanGetEntry => true;
public FileSystemCaseSensitivity CaseSensitivity => FileSystemCaseSensitivity.CaseSensitive;
public bool EmptyDirectoryName => false;
DirectoryEntry rootEntry;
FileEntry fileEntry;
public ExampleFileSystem()
{
DateTimeOffset time = DateTimeOffset.MinValue;
rootEntry = new DirectoryEntry(this, "", "", time, time, null);
fileEntry = new FileEntry(this, "example.txt", "example.txt", time, time, 11L, null);
}
public IDirectoryContent Browse(string path, IOption? option = null)
{
// Browse root
if (path == rootEntry.Path) return new DirectoryContent(this, path, new IEntry[] { fileEntry });
// Not found
return new DirectoryNotFound(this, path);
}
public IEntry? GetEntry(string path, IOption? option = null)
{
// Root entry
if (path == rootEntry.Path) return rootEntry;
// File entry
if (path == fileEntry.Path) return fileEntry;
// Entry not found
return null;
}
public Stream OpenStream(string path, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, IOption? option = null)
{
if (path != fileEntry.Path) throw new FileNotFoundException(path);
if (fileMode != FileMode.Open) throw new NotSupportedException();
if (fileAccess != FileAccess.Read) throw new NotSupportedException();
byte[] data = UTF8Encoding.UTF8.GetBytes("Hello World");
return new MemoryStream(data);
}
public IMemory<byte> OpenMemory(string path, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, bool concurrentUsable = false, IOption? option = null)
{
Stream stream = OpenStream(path, fileMode, fileAccess, fileShare, option);
IMemory<byte> memory = new StreamMemory(stream).AttachDisposable(stream);
if (concurrentUsable) memory = new SynchronizedMemory<byte>(memory);
return memory;
}
}
And to run.
IFileSystem filesystem = new ExampleFileSystem();
Console.WriteLine(filesystem.PrintTree());