MemoryFileSystem

Ecng.IO

In-memory implementation of IFileSystem useful for tests and environments without disk access. Thread-safe for basic operations.

Implements: IFileSystem

Properties

MaxSize
public long MaxSize { get; set; }
value = memoryFileSystem.MaxSize
memoryFileSystem.MaxSize = value

Maximum total size of all files in bytes. Zero or negative means unlimited.

OverflowBehavior
public FileSystemOverflowBehavior OverflowBehavior { get; set; }
value = memoryFileSystem.OverflowBehavior
memoryFileSystem.OverflowBehavior = value

Behavior when MaxSize limit is exceeded.

TotalSize
public long TotalSize { get; }
value = memoryFileSystem.TotalSize

Current total size of all files in bytes.

Methods

CopyFile
public void CopyFile(string sourceFileName, string destFileName, bool overwrite)
memoryFileSystem.CopyFile(sourceFileName, destFileName, overwrite)

Copies a file to a new location.

sourceFileName
Source file path.
destFileName
Destination file path.
overwrite
If true, overwrites destination.
CreateDirectory
public void CreateDirectory(string path)
memoryFileSystem.CreateDirectory(path)

Creates a directory including all missing intermediate directories.

path
Path to the directory.
DeleteDirectory
public void DeleteDirectory(string path, bool recursive)
memoryFileSystem.DeleteDirectory(path, recursive)

Deletes a directory.

path
Path to the directory.
recursive
If true, deletes contents recursively.
DeleteFile
public void DeleteFile(string path)
memoryFileSystem.DeleteFile(path)

Deletes a file.

path
The path to the file.
DeleteFileInternal
private void DeleteFileInternal(string path)
memoryFileSystem.DeleteFileInternal(path)

Internal delete without lock (caller must hold lock).

DirectoryExists
public bool DirectoryExists(string path)
result = memoryFileSystem.DirectoryExists(path)

Checks whether a directory exists at the specified path.

path
Path to the directory.

Returns: True if the directory exists; otherwise, false.

EnumerateDirectories
public IEnumerable<string> EnumerateDirectories(string path, string searchPattern, SearchOption searchOption)
result = memoryFileSystem.EnumerateDirectories(path, searchPattern, searchOption)

Enumerates subdirectories in a directory.

path
Directory path.
searchPattern
Search pattern (supports * and ?).
searchOption
Search scope.

Returns: Sequence of directory paths.

EnumerateFiles
public IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption)
result = memoryFileSystem.EnumerateFiles(path, searchPattern, searchOption)

Enumerates files in a directory.

path
Directory path.
searchPattern
Search pattern (supports * and ?).
searchOption
Search scope.

Returns: Sequence of file paths.

FileExists
public bool FileExists(string path)
result = memoryFileSystem.FileExists(path)

Checks whether a file exists at the specified path.

path
Path to the file.

Returns: True if the file exists; otherwise, false.

GetAttributes
public FileAttributes GetAttributes(string path)
result = memoryFileSystem.GetAttributes(path)

Gets file or directory attributes (equivalent to File.GetAttributes). Implementations should throw appropriate exceptions if the path does not exist.

path
Path to the file or directory.

Returns: The FileAttributes for the given path.

GetCreationTimeUtc
public DateTime GetCreationTimeUtc(string path)
result = memoryFileSystem.GetCreationTimeUtc(path)

Gets the creation time (UTC) of a file or directory.

path
Path.

Returns: Creation time in UTC.

GetEvictableCandidates
private List<Node> GetEvictableCandidates()
result = memoryFileSystem.GetEvictableCandidates()

Collects all file nodes that can be evicted (closed, not read-only).

GetFileLength
public long GetFileLength(string path)
result = memoryFileSystem.GetFileLength(path)

Gets file length in bytes.

path
Path to the file.

Returns: File size in bytes.

GetLastWriteTimeUtc
public DateTime GetLastWriteTimeUtc(string path)
result = memoryFileSystem.GetLastWriteTimeUtc(path)

Gets the last write time (UTC) of a file or directory.

path
Path.

Returns: Last write time in UTC.

MoveDirectory
public void MoveDirectory(string sourceDirName, string destDirName)
memoryFileSystem.MoveDirectory(sourceDirName, destDirName)

Moves a directory to a new location.

sourceDirName
Source directory path.
destDirName
Destination directory path.
MoveFile
public void MoveFile(string sourceFileName, string destFileName, bool overwrite)
memoryFileSystem.MoveFile(sourceFileName, destFileName, overwrite)

Moves a file to a new location.

sourceFileName
Source file path.
destFileName
Destination file path.
overwrite
If true, overwrites destination.
Open
public Stream Open(string path, FileMode mode, FileAccess access, FileShare share)
result = memoryFileSystem.Open(path, mode, access, share)

Opens a stream for the file with specified mode and access.

path
Path to the file.
mode
File open mode.
access
File access type.
share
File sharing mode.

Returns: A stream with the specified access.

SetReadOnly
public void SetReadOnly(string path, bool isReadOnly)
memoryFileSystem.SetReadOnly(path, isReadOnly)

Sets or clears the read-only attribute on the specified file. Implementations should handle cases when the file does not exist gracefully.

path
Path to the file.
isReadOnly
True to set read-only; false to clear.
TryEvictOldest
private bool TryEvictOldest(long requiredSpace)
result = memoryFileSystem.TryEvictOldest(requiredSpace)

Tries to free up space by evicting oldest closed files.

requiredSpace
Amount of space needed.

Returns: True if enough space was freed; false otherwise.