.NET Getting Started VB: File Action in Visual Basic.net (1)

zhaozj2021-02-16  49

There are three ways to access the file system in Visual Basic.NET: The first is to use the Visual Basic runtime function for file access (VB traditional way direct file access); the second is to access the system.io model in .NET; The third is to access the file system object model FSO.

The file is a collection of data stored on some media. In terms of itself, the file is only a series of related data bytes on the disk. When an application access file, it must assume whether the byte indicates characters, data records, integers, strings, and the like. Tell the application to assume what content by specifying the access type of the file.

Visual Basic provides three types of file access:

1. The order is used to read and write text files in a continuous block.

2. Random, for reading and writing a text or binary file for fixed length records.

3. Binary, used to read and write files for any structure.

File access using the Visual Basic runtime function

VB.NET preserves the way VB's early direct file access is to access the operation file directly through some related functions. The following table lists all available file access functions of three direct file access types, respectively.

function

order

random

Binary

Description

FileOpen function

X

X

X

Open the input or output file

FILECLOSE function

X

X

X

Turn off files that open with fileopen functions

INPUT function

X

X

Read the data from the open order file and assign the data to the variable.

INPUTSTRING function

X

Return the string value

LineInput function

X

Read a row of data from the open order file and assign it to the String variable.

Print, Printline function

X

Write the formatted display data to the order file.

Write, WriteLine function

X

Write the data to the order file.

To operate files, first open the file, use the FileOpen function.

Example: Open the readme.txt file under the C drive in an OUTPUT mode sharing.

FileOpen (1, "c: /readme.txt", openmode.output, openshare.shared)

When the operation file is over, you need to turn off the loss of the file content, and it is necessary to turn the file when you need to reopen the file, use the fileclose function, turn off the file C: /Readme.txt that is opened.

FILECLOSE (1);

When the order file is opened in the Input mode, the file to be opened must already exist, otherwise open an error, open a non-existing file in Output or Append mode, FileOpen first created the file first, then open.

Some other functions:

DIR function: Returns a string of file names, directory names, or folder names that match the specified mode or file properties, or a string that returns the drive volume.

EOF function: When the end of the file opened in the Random or Sequential Input mode, returns the boolean value true.

Filecopy function: copy file

FileDateTime function: Returns the Date value indicating the date and time of the creation or final modification file.

Filelen Function: Returns the long value of the specified file length indicated by byte.

FreeFile function: Returns an Integer value indicating the next file number that can be used by the FileOpen function.

Getattr function: Returns the fileAttribute value of the properties of the file, directory, or folder.

Loc function: Returns a long value that specifies the current read / write location in the file.

LOF function: Returns a long value, indicating the size of the file opened with the FileOpen function (in bytes). Seek Function: Returns a long value, specify the current read / write location in the file opened with the FileOpen function, or set the location of the next read / write operation in the file opened with the FileOpen function.

SetAttr function: Set file properties information.

1. Operation of sequential files

Read the string from the file

There are three functions to read strings from sequential files: Input, InputString, and LineInput.

INPUT function

Read the data from the open order file and assign the data to the variable.

Public Sub INPUT (Filenumber AS INTEGER, BYREF VALUE As Object)

The parameter filenumber is a valid file number; Value is a variable that is given the value read from the file, which cannot be an array or object variable.

The following example is a Hello, World! Example of writing and reading text example.txt, new console application, write the following code to view the results.

FileOpen (1, "c: /example.txt", openmode.output)

Write (1, "Hello,")

Write (1, "world!")

FILECLOSE (1)

DIM S As String

FileOpen (1, "c: /example.txt", OpenMode.input)

Do While Not Eof (1)

INPUT (1, s)

Console.writeline (s)

Loop

FILECLOSE (1)

INPUTSTRING function

Returns the String value that contains characters in the file opened in Input or Binary mode.

InputString (Byval Filenumber AS Integer, Byval Charcount As Integer) AS String

The parameter filenumber is a valid file number, and Chargen is a valid numerical expression that specifies the number of characters to be read.

Unlike the Input function, the inputstring function returns all characters it read, including commas, carriage returns, newline characters, quotes, and preambles. For files opened in binary access mode, if you try to read the entire file with the inputString function before EOF returns TRUE, an error is generated. When reading binary files with InputString, use the LOF and LOC functions instead of the EOF function, and use the Fileget function when using the EOF function.

Example:

DIM S As String

FileOpen (1, "c: /example.txt", openmode.binary)

Do While Not Eof (1)

S = InputString (1, 9)

Console.writeline (s)

Loop

FILECLOSE (1)

LineInput function:

Read a row of data from the open order file and assign it to the String variable.

Public Function LineInput (Byval Filenumber As Integer) AS String

Parameter filenumber is a valid file number.

This function reads a character from a file until a carriage return (CHR (13)) or carriage return -oni (CHR (13) chr (10)). Enter-a wrap sequence is skipped instead of attaching to a character string.

Example:

DIM S As String

FileOpen (1, "c: /example.txt", openmode.binary) Do while not Eof (1)

s = lineInput (1)

Console.writeline (s)

Loop

FILECLOSE (1)

Write a string into a file

If you want to write data to the file, you can use the Print / Printline function, if the data to be written is a string or value, you can use the Write / WriteLine function, to write the file, you should first use the file in Output or Append method. Open, then dishes can use the Print function or Write function.

Print / Printline function: Write the formatted display data to the order file.

Public Sub Print (Byval Filenumber As Integer, Byval Paramarray Output () AS Object)

Public Sub Printline (Byval Filenumber As Integer, Val Paramarray Output () AS Object)

The parameter filenumber is a valid file number. Output is a zero or more expressions to write to a comma.

Example:

FileOpen (1, "c: /example.txt", openmode.output)

Printline (1)

Printline (1, "Hello,")

Print (1, SPC (4), "World!")

FILECLOSE (1)

WRITE / WRITELINE and Print / Printline Similar

2. Random file operation

The bytes in the random access file constitute the same record, each record contains one or more fields, and for a field record corresponding to any standard type, all records in the random access file must have the same length, if actual The character string contains less than a fixed length of the string element that is written to it, and Visual Basic fills the trailing space in the record in the record. If the string is long, Visual Basic truncates it.

Example: User Defines Data Type:

Structure Person

Public ID as integer

Public MONTHLYSALY As Decimal

Public Name As String

Public SEX As String

End structure

Where vbfixedstring is used to define strings of strings as fixed lengths.

You should define a type of this type that is included or not included in the type of files that should be included in the file before opening a file.

Open the file to be randomly accessed:

Fileopen function: FileOpen (Filenumber, FileName, OpenMode.random,, RECORDLENGTH)

Filenumber and FileName specify the number and file name of the file to be opened. Recordlength Specifies the size of each record in bytes in bytes. If RecordLength is less than the actual length of the record of the write file, an error is generated.

Example: Open a file with a random access.

DIM Filenum As Integer, RecLength As Long, APERSON As Person

'Calculate the length of record

RecLength = LEN (APERSON)

'Get available file numbers

FILENUM = Freefile ()

'open a file

FileOpen (Filenum, "C: /example.txt", OpenMode.random,, Replace Once, you can use the fileget function after opening the file after opening the file.

Example: Read the first record from the file

DIM Filenum As Integer, RecLength As Long, APERSON As Person

RecLength = LEN (APERSON)

FILENUM = Freefile ()

FileOpen (Filenum, "C: /example.txt", OpenMode.random,, Replaceng

Fileget (Filenum, APERSON, 1);

Console.writeLine (APERSON.ID)

Console.writeLine (APERSON.NAME)

Console.writeline (APERSON.SEX)

Console.writeline (APERSON.MONTHLYSAlary)

FILECLOSE (filenum)

Write record: Replace the existing record or new record via the Fileput function

Example: Write five records to the file:

'Custom data type

Structure Person

Public ID as integer

Public Name As String

End structure

Sub writedata ()

DIM MyRecord As Person

Dim RecordNumber As INTEGER

'Randomly opened.

FileOpen (1, "c: /example.txt", openmode.binary)

For RecordNumber = 1 to 5 '

MyRecord.id = RecordNumber 'Define ID.

MyRecord.name = "My Name" & RecordNumber "Creates a string

Fileput (1, myRecord) wrote

Next RecordNumber

FILECLOSE (1)

End Sub

The above code implements a random access to the file and writes a record.

Operation of binary files

Open files for binary access

FileOpen (Filenumber, FileName, OpenMode.Binary)

Close the file to be binary access

FILECLOSE (Filenumber)

If you keep a small file size, binary access is used. Since binary access does not require a fixed length field, the type declaration can omit the string length parameters. This makes the disk space by generating a beam record. The type of binary access is defined as:

Structure Person

DIM ID As INTEGER

DIM Name as String

Dim MonthlySalary As Decimal

DIM SEX As String

End structure

The disadvantage of binary input / output using the variable length field is that the record cannot be randomly access, but to access them in order to understand the length of each record. You can still go directly to the specified byte location in the file, but if the field is growing, you don't know which record is located at which one.

(Some translated from MSDN)

转载请注明原文地址:https://www.9cbs.com/read-24587.html

New Post(0)