Summary of file objects in ASP. NET (hidden)

Source: Internet
Author: User
The namespace is introduced in ASP. NET. The namespace corresponding to file operations is System. Io . The namespace is similar to the dynamic link library, but it can be applied in the entire. Net System. On the ASP. NET page, you can use <% @ import namespace =... %> to reference the namespace. In VB6, FSO is used to operate files. The file objects in Asp.net are similar to FSO.

Here I will introduce the file object:File objects are mainly used to create file stream objects. You can use file objects to comprehensively manage system files, and create, copy, delete, move, and open files.

Next, we will first create a file object.

In Asp.net, you can use two 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 dim mikecatfile as filemikecatfile = new file ("C: \ mikecat.txt ")

Please note that the VB syntax used in Asp.net does not need to be used for object assignment.SetStatement

Main attributes of the file object

When performing file read/write operations on a file object, you only need to use two attributes:LengthAndNameThey are the file length and name respectively.

Main method of file object

There are nine common methods for file objects

 
(1) Create method this method is used to create a new file function create (string) as filestream Where string is the path of the file to be created, and the filestream object is returned, this object provides a channel for reading and writing files.

 
(2) Delete method this method is used to delete an existing file 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. 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 to specify the path of the source file, destination to specify the path of the target file. The copy method only allows copying an existing file to a new file, and does not allow overwriting of an existing file.

 
(4) createtext method this method is used to create a new file, which can be written. Function createtext (string) as streamwriter or function createtext () as streamwriter returns the streamwriter object, which is used to perform write operations on files. Unlike the create method, the createtext method returns not a filestream object, it is a streamwriter object.

(5) fileexists method this method is used to check whether the specified file has function fileexits (string) as Boolean string to specify the path of the file to be searched. If the file exists, the return value is true; otherwise, the return value is false.
 
(6) getextension method this method is used to obtain the object extension function getextension (string) as string. If the specified object has an extension, the extension string is returned, which also contains ". "If the specified file does not contain an extension, for example, if the string parameter specifies the path of a folder, empty is returned.

 
(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, and destination specifies the path of the target file. Unlike the copy method, the source file is deleted after the move method is used.

(8) open method function open (filename, filemode, fileaccess) as filestream this method returns the File Operation Channel object filestream. Filemode and fileaccess define a group of enumeration constants in the system. Io namespace to specify the file operation mode and operation permissions. Filemode. append: open the file by append, or create a new file by append. Use this modeCompositionAnd fileaccess. write is used together, that is, you must have the write permission filemode. create creates a new file. If a file with the same name exists, it overwrites the original file filemode. createnew creates a new file. If a file with the same name exists, an error occurred while opening the file: filemode. open open an existing file filemode. openorcreate open an existing file. If the file does not exist, create a new file. Filemode. truncate clears all the content of the file when the file is opened. If you use this attribute, you must have at least the write permission for the file fileaccess. only the file opened by read has the permission to read fileaccess. the file opened by write only has the write permission fileaccess. the files opened by readwrite can be written or read.

 
(9) opentext method this method is used to create a streamreader object and read existing files. Function opentext (string) as streamreader or function opentext () as streamreader is different from the open method. opentext returns a streamreader object, which is used to read files.
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.