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.
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.
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.
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.