Interface IFileChannels
The file numbers a session has open (MS-VBAL §5.4.5) — the shim every file statement goes through.
public interface IFileChannels
Remarks
🎯 A single seam on purpose, and not only because twelve statements share it. File I/O is the most consequential thing a VBA program does to the machine it runs on, so it is the thing an administrator most needs to be able to see, restrict, or redirect, and the thing a test most needs to be able to fake. Every one of those is a matter of which implementation the session was composed with rather than of anything the statements know.
The file system is already abstracted platform-wide (System.IO.Abstractions), so what this
adds is the part VBA has and a file system does not: numbered channels, the modes they were opened under,
and the rules about which statement may use which.
⚖️RDCore provides an implementation of this interface licensed under GPLv3.
Properties
Open
The channels currently open, in no particular order.
IEnumerable<IFileChannel> Open { get; }
Property Value
Methods
Close(int)
Disassociates fileNumber, closing the file
(MS-VBAL §5.4.5.2).
bool Close(int fileNumber)
Parameters
fileNumberintThe file number to close.
Returns
- bool
falsewhen it was not open.
CloseAll()
Closes every open channel — what a Close with no file number, and a Reset, both do
(MS-VBAL §5.4.5.2).
int CloseAll()
Returns
- int
How many were closed.
TryChangeDirectory(string)
Changes the current directory (ChDir, MS-VBAL §6.1.2.5.2.1).
VBRuntimeErrorId? TryChangeDirectory(string path)
Parameters
pathstring
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:76for a directory that does not exist.
TryChangeDrive(string)
Changes the current drive (ChDrive, MS-VBAL §6.1.2.5.2.2): an empty one changes nothing.
VBRuntimeErrorId? TryChangeDrive(string drive)
Parameters
drivestring
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:68for a letter that is no drive,5for a drive that is no letter.
TryCopy(string, string)
Copies a file (FileCopy, MS-VBAL §6.1.2.5.2.3), replacing the destination.
VBRuntimeErrorId? TryCopy(string source, string destination)
Parameters
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:52,53,55,75,76(IStdFileSystemModule.FileCopy).
TryCreateDirectory(string)
Creates a directory (MkDir, MS-VBAL §6.1.2.5.2.5).
VBRuntimeErrorId? TryCreateDirectory(string path)
Parameters
pathstring
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:75when something of the name exists,76when the directory it is in does not.
TryDelete(string)
Deletes the files that match pattern (Kill, MS-VBAL §6.1.2.5.2.4): not a directory, nor a hidden or a
system file.
VBRuntimeErrorId? TryDelete(string pattern)
Parameters
patternstring
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:53,55,75(IStdFileSystemModule.Kill).
TryFindFirst(string, VBFileAttribute, out string)
The first name that matches pattern (Dir, MS-VBAL §6.1.2.5.1.2), and what the next TryFindNext(out string)
goes on from.
VBRuntimeErrorId? TryFindFirst(string pattern, VBFileAttribute attributes, out string name)
Parameters
patternstringA file name with wildcards, that can include a directory and a drive.
attributesVBFileAttributeThe attributes, besides the normal ones, of what matches.
namestringThe name, with no directory part; empty when nothing matches.
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null.
TryFindNext(out string)
The next name that matches the pattern TryFindFirst(string, VBFileAttribute, out string) was last given.
VBRuntimeErrorId? TryFindNext(out string name)
Parameters
namestringThe name; empty when there is no other.
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:5when there is no pattern to go on with.
TryGet(int, out IFileChannel?)
The channel fileNumber is open on.
bool TryGet(int fileNumber, out IFileChannel? channel)
Parameters
fileNumberintThe file number source referred to.
channelIFileChannelThe channel it is open on.
Returns
- bool
falsewhen that file number is not currently open.
TryGetAttributes(string, out VBFileAttribute)
The attributes of a file or a directory (GetAttr, MS-VBAL §6.1.2.8.1.8).
VBRuntimeErrorId? TryGetAttributes(string path, out VBFileAttribute attributes)
Parameters
pathstringattributesVBFileAttribute
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:53for what does not exist,52for a pattern.
TryGetCurrentDirectory(string?, out string)
The current directory of a drive (CurDir, MS-VBAL §6.1.2.5.1.1).
VBRuntimeErrorId? TryGetCurrentDirectory(string? drive, out string path)
Parameters
drivestringThe drive, its first letter being what counts; null or empty for the current drive.
pathstringThe directory.
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:68for a letter that is no drive,5for a drive that is no letter.
TryGetLastWriteTime(string, out DateTime)
When a file or a directory was last modified (FileDateTime, MS-VBAL §6.1.2.5.1.5), local time.
VBRuntimeErrorId? TryGetLastWriteTime(string path, out DateTime modified)
Parameters
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:53for what does not exist,52for a pattern.
TryGetLength(string, out long)
The length of a file on the file system, in bytes (FileLen, MS-VBAL §6.1.2.5.1.6): 0 for a directory.
VBRuntimeErrorId? TryGetLength(string path, out long length)
Parameters
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:53for what does not exist,52for a pattern.
TryOpen(int, string, VBFileMode, VBFileAccessMode, VBFileLockMode, int)
Associates fileNumber with path
(MS-VBAL §5.4.5.1).
VBRuntimeErrorId? TryOpen(int fileNumber, string path, VBFileMode mode, VBFileAccessMode access, VBFileLockMode @lock, int recordLength)
Parameters
fileNumberintThe file number to associate. Must not already be open.
pathstringThe complete path specification.
modeVBFileModeThe mode, defaulted by the caller from the statement's own clauses.
accessVBFileAccessModeThe access, defaulted by the caller from
mode.lockVBFileLockModeThe lock, Shared when the statement declared none.
recordLengthintThe
Len =record length, or0.
Returns
- VBRuntimeErrorId?
The error that stopped it, or
nullwhen the channel is open. The specification names which:55when the file number is already open,53when an Input names no existing file,55when an Append/Output names a file another channel already has open,70when the requested lock cannot be had, and75when the file cannot be created.
Remarks
Creates the external file when it does not exist, unless the mode is Input, which the specification says is an error instead.
TryRemoveDirectory(string)
Removes an empty directory (RmDir, MS-VBAL §6.1.2.5.2.6).
VBRuntimeErrorId? TryRemoveDirectory(string path)
Parameters
pathstring
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:75for one that is not empty,76for one that does not exist.
TryRename(string, string)
Renames a file or a directory, moving it when the new path is somewhere else (Name … As …; see
RD-VBAL §5.4.5.13).
VBRuntimeErrorId? TryRename(string oldPath, string newPath)
Parameters
oldPathstringThe path of the existing file or directory.
newPathstringThe path it is to have, which must not exist.
Returns
- VBRuntimeErrorId?
The error that stopped it, or
nullwhen it was renamed:52when either path has a wildcard in it,53whenoldPathnames nothing,55when it is open,58whennewPathexists,74when a directory would change drive,76when a directory the paths name does not exist,70when access is refused, and75when anything else about the path or the device stops it.
Remarks
The channels are the ones that know which files are open, which is why this is theirs and not the file system's: a file that has a file number association is not renamed. Neither path is a pattern.
TrySetAttributes(string, VBFileAttribute)
Sets the attributes of a file (SetAttr, MS-VBAL §6.1.2.5.2.7).
VBRuntimeErrorId? TrySetAttributes(string path, VBFileAttribute attributes)
Parameters
pathstringattributesVBFileAttribute
Returns
- VBRuntimeErrorId?
The error that stopped it, or
null:5,52,53,55(IStdFileSystemModule.SetAttr).