Export (0) Print
Expand All

FileSystem.FileGetObject Method

Reads data from an open disk file into a variable. The My feature gives you better productivity and performance in file I/O operations than FileGetObject. For more information, see FileSystem.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static void FileGetObject(
	int FileNumber,
	ref Object Value,
	long RecordNumber
)

Parameters

FileNumber
Type: System.Int32

Required. Any valid file number.

Value
Type: System.Object

Required. Valid variable name into which data is read.

RecordNumber
Type: System.Int64

Optional. Record number (Random mode files) or byte number (Binary mode files) at which reading starts.

The FileGetObject function is used instead of FileGet to avoid ambiguities at compile time if type Object is returned instead of another type, such as Integer, Long, Short, and so forth.

If you intend to write out the Variant type, FileGetObject is required. When in doubt, if you are using an object for the second parameter, it is always suggested that you use FilePutObject and FileGetObject.

FileGetObject is valid only in Random and Binary mode.

Data read with FileGetObject is usually written with FilePutObject.

The first record or byte in a file is at position 1, the second record or byte is at position 2, and so on. If you omit RecordNumber, FileGetObject reads the record or byte after the last FileGetObject or FilePutObject function (or pointed to by the last Seek function).

Random Mode

For files opened in Random mode, the following rules apply:

  • If the length of the data being read is less than the length specified in the RecordLength clause of the FileOpen function, FileGetObject reads subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data cannot be precisely determined, it is a good idea to have the record length match the length of the data being read.

  • If the variable being read into is a string, by default FileGetObject reads a two-byte descriptor containing the string length and then reads the data that goes into the variable. Therefore, the record length specified by the RecordLength clause of the FileOpen function must be at least two bytes greater than the actual length of the string. Visual Basic 6.0 and earlier versions support fixed-length strings and when read to a file, the length descriptor is not written. If you want to read a string without the descriptor, you should pass True to the StringIsFixedLength parameter, and the string you read into should be the correct length.

  • If the variable being read into is an array, then the record length specified by the RecordLength parameter in the FileOpen function must be greater than or equal to the sum of all the bytes required to write the array data and the array descriptor. The descriptor specifies the rank of the array, the size, and the lower bounds for each rank. Its length equals 2 plus 8 times the number of dimensions: 2 + 8 * NumberOfDimensions.

    For example, the following array declaration requires 218 bytes when the array is written to disk:

    Dim MyArray(4, 9) As Integer
    

    The 218 bytes are distributed as follows: 18 bytes for the descriptor (2 + 8 * 2), and 100 bytes for the data (5 * 10 * 4).

  • FileGetObject reads elements of structures as if each were being read individually, except that there is no padding between elements. On disk, a dynamic array in a user-defined type (written with FilePutObject) is prefixed by a descriptor whose length equals 2 plus 8 times the number of dimensions: 2 + 8 * NumberOfDimensions. The record length specified by the RecordLength clause in the FileOpen function must be greater than or equal to the sum of all the bytes required to read the individual elements, including any arrays and their descriptors. The VBFixedStringAttribute class can be applied to string fields in the structures to indicate the size of string when written to disk.

Binary Mode

For files opened in Binary mode, all of the Random rules apply, with these exceptions:

  • The RecordLength clause in the FileOpen function has no effect. FileGetObject reads all variables from disk contiguously, that is, with no padding between records.

  • For any array other than an array in a structure, FileGetObject reads only the data. No descriptor is read.

FileGetObject reads variable-length strings that are not elements of structures without expecting the two-byte length descriptor. The number of bytes read equals the number of characters already in the string.

Security noteSecurity Note

When reading from files, do not make decisions about the contents of a file based on the file name extension. For example, a file named Form1.vb may not be a Visual Basic source file.

The following example reads a record into a test file and then retrieves it.

Dim c As Object = "test"
FileSystem.FileOpen(1, "test.dat", OpenMode.Binary)
FileSystem.FilePutObject(1, "ABCDEF")
FileSystem.Seek(1, 1)
FileSystem.FileGetObject(1, c)
MsgBox(c)
FileSystem.FileClose(1)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft