Asp. Summary of File objects in net

Source: Internet
Author: User
Tags contains empty
The Asp.net| object introduces a namespace in ASP.net, where the name space corresponding to the file operation is System.IO. Namespaces and dynamic link libraries are somewhat similar, but namespaces can be applied throughout the. NET System. In asp.net pages, you can use <% @import namespace=...%> to refer to the namespace. The FSO is used in VB6 to operate the file, and the file object in ASP.net is similar to the FSO.

Here I introduce the file object: The main function of the file object is to create a stream object. File objects enable you to manage system files in a comprehensive way, creating, copying, deleting, moving, and opening files

Below our door first create a file object.

In asp.net, you can use 2 methods to create a file object.


(1) Use the Dim statement to create a file object
Dim mikecatfile As File
Mikecatfile.create ("C:\mikecat.txt")



(2) Use the new statement to create the file object
Dim mikecatfile As New File ("C:\mikecat.txt")

You can also define this
Dim mikecatfile As File
Mikecatfile=new file ("C:\mikecat.txt")



Please note that the VB syntax used in asp.net no longer requires the use of SET statements when assigning values to objects

Main properties of the file object

When you read and write files to a file object, you use only 2 properties: Length and name are file lengths and file names, respectively.

The main method of the File object

The file object has 9 common methods


(1) Create method This method is used for creating a new file
function Create (string) as FileStream
Where string is the path to the file that you want to create, and returns the FileStream object, which provides a channel for reading and writing the file


(2) Delete method This method is used to delete a file that already exists
Sub Delete (string)
or sub Delete ()
If the file object contains files parameters, you can use the second method to delete the file. Otherwise, only the first method can be used to delete the file, and the string parameter specifies the path of the file 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 origin file, destination the path of the specified destination file. The Copy method only allows you to copy an existing file to a new file and not allow overwriting of an existing file.



(4) CreateText method
This method is used to create a new file that can be written.
function CreateText (string) as StreamWriter
or function CreateText () as StreamWriter
The return value is the StreamWriter object used to perform write operations on the file
Unlike the Create method, the CreateText method returns not the FileStream object, but the StreamWriter object.



(5) FileExists method
This method is used to check whether the specified file exists
function Fileexits (String) As Boolean
string specifies the path of the file to look for, or False if the file exists and the return value is True




(6) GetExtension method
This method is used to get the file extension
function GetExtension (string) As String
If the specified file has an extension, the extension string is returned, and the string also contains the "."
Returns empty if the specified file does not contain an extension, such as the path of a folder that is specified by the string parameter.



(7) Move method
This method is used to move an existing file to a new folder.
Sub Move (source,destination)
SOURCE specifies the path to the source file, destination the path to the specified destination file, and the copy method is deleted after the Move method is used.



(8) Open method
function open (filename,filemode,fileaccess) as FileStream
This method returns the file Action channel object FileStream.
FileMode and FileAccess each define a set of enumerated constants in the System.IO namespace that specify the file operation mode and Operation permissions.
Filemode.append Open the file as an append, or create a new file in an append way. When you use this mode to manipulate a file, you must use it with FileAccess.Write, which means that you must have write permission
FileMode.Create creates a new file that overwrites the original file if a file with the same name exists
FileMode.CreateNew Create a new file, if there is a file with the same name, open file error
FileMode.Open open a file that already exists
FileMode.OpenOrCreate opens a file that already exists and creates a new file if it does not exist.
Filemode.truncate to empty all the contents of a file when the file is opened, if you use this property to have at least write permission to the file

FileAccess.Read open files only Read permission
FileAccess.Write open files only Write permission
FileAccess.ReadWrite open files can be written to or read from



(9) OpenText method
This method is used to create a StreamReader object and read operations on files that already exist.
function OpenText (string) as StreamReader
or function OpenText () as StreamReader
Unlike the Open method, OpenText Returns a StreamReader object that is used to perform read operations on the file.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.