C # File Reading (1)

Source: Internet
Author: User

1. Steps for reading and writing files:

 

Create a file stream-create the corresponding reader-Execute read/write operations-close the reader-close the file stream

 

Create a file stream: filestream objfs = new filestream (file path, file opening mode );

File opening mode: filemode is an enumeration value, which is omitted here

Colleagues who create files can also set permissions.

Create a reader: streamreader objsr = new streamreader (objfs, encoding. utf8 );

Streamwriter objsw = new streamwriter (objfs, encoding. utf8 );

Enoding is used to solve the garbled problem.

Read/write operations: Read: objsr. readtoend ();

Write: objsw. Write (content );

 

Close reader: objsr. Close ();

Objsw. Close ();

 

Close file stream: objfs. Close ();

 

 

2. File class

 

Provides various methods for operating files.

Method

Description

Bool exists (string path)

Checks whether a specified file exists. This method returns a Boolean value.

Void copy (string

Sourcefilename,
String destfilename)

Copy the content in the source file of the specified path to the target file. If the target file does not exist, create a new file in the specified path.

Void move (string

Sourcefilename,
String destfilename)

Move the specified file to a new path

Void Delete (string path)

Delete the specified file,If the specified file does not exist, no exception is thrown.

 

3. The Directory class allows you to move or delete folders.

Method

Description

Bool exists (string path)

Used to check whether a specified folder exists on the disk

Void move (string

Sourcedirname,

String destdirname)

Used to move files or directories and their contents to a new location.

Void Delete (string path,

Bool recursive)

Delete the specified directory. If boolTrueDelete all contents in the subdirectory.

Void Delete (string path)

Delete an empty directory from a specified path

3. fileinfo class

Because the static methods of the file class are used for security check, if you want to use a file object multiple times, you can use the corresponding instance method of the fileinfo class because security check is not always required.

 

Attribute/Method

Description

Exists

Checks whether a specified file exists and returns a Boolean value.

Extension

Obtain a string that represents the file extension.

Name

Get File Name

Fullname

Obtain the complete directory of a directory or file

Fileinfo copyto (string)

Copying existing files to new files does not allow overwriting of existing files

Void Delete ()

Permanently delete an object

Void moveTo (string)

Move the specified file to a new location

 

4. directoryinfo class

If you want to reuse a directory object multiple times, consider using the instance method of the directoryinfo class.

Method

Description

Directoryinfo [] getdirectories ()

Returns the array of subdirectory objects in the current directory.

Fileinfo [] getfiles ()

Returns the list of files in the current directory (fileinfoObject array)

 

 

Note: file and stream)

A file is an ordered collection of data permanently stored on various media. It is a basic object for data read/write operations.

Stream, in terms of concept, is very similar to a single disk file. It is also the basic object for data reading. The Stream provides us with a continuous byte stream storage space.

File class is a static class mainly used to provide some function libraries.

The static objects class provides many static methods that support basic file operations, including creating, copying, moving, deleting, and opening a file.

C # File Reading (1)

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.