Table of Contents

Interface IFileChannels

Namespace
RDCore.SDK.Runtime.Abstract.Execution
Assembly
RDCore.SDK.dll

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

IEnumerable<IFileChannel>

Methods

Close(int)

Disassociates fileNumber, closing the file (MS-VBAL §5.4.5.2).

bool Close(int fileNumber)

Parameters

fileNumber int

The file number to close.

Returns

bool

false when 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

fileNumber int

The file number source referred to.

channel IFileChannel

The channel it is open on.

Returns

bool

false when 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

fileNumber int

The file number to associate. Must not already be open.

path string

The complete path specification.

mode VBFileMode

The mode, defaulted by the caller from the statement's own clauses.

access VBFileAccessMode

The access, defaulted by the caller from mode.

lock VBFileLockMode

The lock, Shared when the statement declared none.

recordLength int

The Len = record length, or 0.

Returns

VBRuntimeErrorId?

The error that stopped it, or null when the channel is open. The specification names which: 55 when the file number is already open, 53 when an Input names no existing file, 55 when an Append/Output names a file another channel already has open, 70 when the requested lock cannot be had, and 75 when 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

oldPath string

The path of the existing file or directory.

newPath string

The path it is to have, which must not exist.

Returns

VBRuntimeErrorId?

The error that stopped it, or null when it was renamed: 52 when either path has a wildcard in it, 53 when oldPath names nothing, 55 when it is open, 58 when newPath exists, 74 when a directory would change drive, 76 when a directory the paths name does not exist, 70 when access is refused, and 75 when 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.