Summary of File objects in ASP.NET

zhaozj2021-02-16  51

Mike Cat (Mikecat)

From: Old cat の Ide

In the ASP.NET, namespaces are introduced, where the namespace corresponding to the file operation is System.IO. The namespaces and dynamic link libraries are somewhat similar, but the namespace can be applied throughout the .NET system. You can use <% @ Import Namespace = ...%> in the page of ASP.NET. Fido is used in VB6 to operate files, and file objects and FSOs in ASP.NET are very similar. Here I introduce the File object: The role of the File object is mainly to create a file stream object. By File objects, you can fully manage system files, you can create, copy, delete, move, and open files below my door first created a File object. In ASP.NET, you can use two methods to create a File object.

(1) Use a DIM statement to create a File object

Dim Mikecatfile As File

Mikecatfile.create ("c: /mikecat.txt")

(2) Use the New statement to create a File object

Dim Mikecatfile As New File ("c: /mikecat.txt")

Can also be defined

Dim Mikecatfile As File

Mikecatfile = new file ("c: /mikecat.txt")

Everyone please note that the VB syntax used in ASP.NET is no longer needed when performing object assignments.

SET statement

The main properties of the file object

When reading and writing, using two attributes for file objects:

Length and

Name is the length and file name of the file, respectively.

The main method of FILE object

File objects have 9 common methods

(1) CREATE method This method is used to create a new file

Function Create (String) AS FileStream

Where string is the path to the file you want, return to the FileStream object, this object provides channels for the reading and writing of the file.

(2) DELETE method This method is used to delete a file already existing.

SUB Delete (String)

Or SUB Delete ()

If the File object contains file parameters, you can use the second method to delete the file. Otherwise, you can only use the first method to delete the file, and the String parameter specifies the file path to be deleted.

(3) COPY method

This method is used to copy a file to a new file.

Sub Copy (Source, Destination)

Source Specifies the path to the source file, the Destination specifies the path to the destination file. The copy method only allows you to copy an already existing file to a new file, which is not allowed to overwrite an existing file.

(4) CREATEXT method

This method is used to create a new file that can be written.

Function CreateText (String) AS Streamwriter

Or Function CreateText () as streamwriter

Return the value for the StreamWriter object to perform a write operation on the file

And the Create method is not the same, the CreateText method is not a FileStream object, but a StreamWriter object.

(5) FileExists method

This method is used to check if the specified file exists.

Function FileExits (String) As Boolean

String Specifies the file path to find, if the file exists, the return value is True, otherwise false

(6) getExtension method

This method is used to obtain the extension of the file

Function getExtension (String) AS STRING

If the specified file has an extension, the extension string is returned, and the string also contains "." If the specified file does not include an extension, if the String parameter specifies the path to a folder, return EMPTY

(7) MOVE method

This method is used to move an existing file to a new folder.

Sub Move (Source, Destination)

Source Specifies the path of the source file, the Destination specifies the path of the destination file, and the copy method is different, and the source file is removed using the MOVE method.

(8) OPEN method

Function Open (FileName, Filemode, FileAccess) AS FileStream

This method returns the file operation channel object FileStream.

FileMode and FileAccess each define a set of enumerations in the System.io namespace, used to specify file operating mode and operational permissions.

FileMode.Append The file is opened in an appended manner, or create a new file in appended manner. When using this mode to operate file, you must use FileAccess.Write, that is, you must have write permission.

FileMode.create creates a new file, if there is a file with the same name, the original file will be overwritten

FileMode.createNew Creates a new file, if there is a different file, open the file error

FileMode.Open opens an existing file

FileMode.Openorcreate opens an already existing file, if the file does not exist, create a new file.

FileMode.truncate Clears all the contents of the file when the file is turned on, if this property is used to write at least write permissions

FileAccess.Read Opened Files Read Permissions

FileAccess.write opens only the permissions written

FileAccess.Readwrite opens files can also be written or read

(9) OpenText method

This method is used to create a StreamReader object and read an existing file.

Function OpenText (String) AS StreamReader

Or Function OpenText () AS StreamReader

And the Open method is that OpenText returns the StreamReader object, which is used to perform a read operation on the file.

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

New Post(0)