C # File and FileInfo

Source: Internet
Author: User

Differences between files and I/O streams:


A file is an ordered and named collection of bytes with permanent storage and specific sequence.
Therefore, for files, we often think of directory paths, disk storage, files, and directory names.
The I/O Stream provides a backup storage for writing bytes and reading bytes from the backup storage.
Backup storage can be one of multiple storage media.


Common classes in the System. IO namespace:


BinaryReader reads the basic metadata type as a binary value using a specific encoding.
BinaryWriter writes the primitive type to the stream in binary format and supports writing strings with specific encoding.
Directory is a static method used to create, move, and enumerate directories and subdirectories.
DirectoryInfo exposes the instance methods used to create, move, and enumerate directories and subdirectories, and cannot be inherited.
File provides static methods for creating, copying, deleting, moving, and opening files, helping you create FileStream objects.
FileInfo provides instance methods for creating, copying, deleting, moving, and opening files, and helps create FileStream objects.
FileStream is a file-based Stream that supports both synchronous read/write operations and asynchronous read/write operations.
FileSystemInfo provides a base class for FileInfo and DirectoryInfo objects.
StreamReader implements a TextReader so that it can read characters from the byte stream with a specific encoding.
StreamWriter implements a TextWriter so that it writes characters to the stream in a specific encoding.






1. File class:


Copy: Copy existing files to new files.

Create a file in the specified path
Delete: Delete the specified file. If the specified file does not exist, no exception is caused.
Exists determines whether the specified file Exists
Move Moves the specified file to a new location and provides the option to specify a new file name
Open the FileStream in the specified path
GetCreationTime returns the creation date and time of the specified file or directory.
OpenRead open an existing file to read
OpenText open an existing UTF-8 encoded text file for reading
OpenWrite open existing files for writing




2. FileInfo class:

Directory: obtains the parent Directory instance.

Exists obtains the value indicating whether the file Exists.

FullName: Obtain the complete directory of a directory or file.

Length: obtains the size of the current file.

Get File Name



3. Basic operations for the File class and FileInfo class


Note:

Since all the methods in the File class are static, if you only want to execute one operation, the efficiency of using methods in the File class may be higher than that in the corresponding FileInfo class.


All methods in the File class are static methods. You must perform a security check on all methods during use. Therefore, if you want to reuse an object multiple times, you can use the corresponding method in the FileInfo class because security check is not always required.




(1) Determine whether the file Exists


Return bool type. If path is empty or has no permission, false is returned.

File: There is no test.txt File in the Croot directory:

File. Exists ("C: \ test.txt ")


FileInfo:

FileInfo fileinfo = new FileInfo ("C: \ test.txt ")

If (fileinfo. Exists ){}




(2) Create a file using the Create Method


The Create method of the File class contains 4 overload methods:

Public static FileStream Create (string path)

Public static FileStream Create (string path, int bufferSize)

Public static FileStream Create (string path, int bufferSize, FileOptions options)

Public static FileStream Create (string path, int bufferSize, FileOptions options, FileSecurity fileSecurity)


File-type Create method parameter description

Path file name

BufferSize: the number of bytes in the buffer zone used to read and write files.

One of the options FileOptions values, which describes how to create or rewrite the file

FileSecurity: One of the FileSecurity values. It determines the access control and audit security of files.


File class:

File. Create ("C: \ test.txt ")


FileInfo class:

FileInfo fileinfo = new FileInfo ("C: \ test.txt ")

Fileinfo. Create ();




(3) Copy methods of the File class for copying files and CopyTo methods of the FileInfo class


Copy method of File class or CopyTo method of FileInfo class

There are two methods to reload the File Copy method.

Public static void Copy (string sourceFileName, string destFileName)

Public static void Copy (string sourceFileName, string destFileName, bool overwrite)

SourceFileName indicates the file to be copied, and destFileName indicates the name of the target file. It cannot be a directory. If it is the first overload, the target file parameter cannot be an existing file.

Overwrite indicates whether the target file can be rewritten.


Copy the test.txt text file under the C root directory to the root directory of drive D:

(This comment indicates that no test.txt file exists in the ddisk root directory)

File. Copy ("C: \ test.txt", "D: \ test.txt ")


The CopyTo method of the FileInfo class can also be overloaded in two ways.

Public FileInfo CopyTo (string destFileName)

Public FileInfo CopyTo (string destFileName, bool overwrite)


FileInfo fileinfo = new FileInfo ("C: \ test.txt ");

Fileinfo. CopyTo ("D: \ test.txt", true );




(4) use the Move method of the File class for moving files and the MoveTo method of the FileInfo class.


File-Move method-

Public static void Move (string sourceFileName, string destFileName)

File. Move ("C: \ test.txt", "D: \ test.txt ")


FileInfo class-MoveTo method-publicvoid MoveTo (string destFileName)

FileInfo fileinfo = new FileInfo ("C: \ test.txt ");

Fileinfo. MoveTo ("D: \ test.txt ");




(5) Delete a file


File-Delete method-public staticvoid Delete (string path)

File. Delete ("C: \ test.txt ")


FileInfo-Delete method-publicvoid Delete (string path)

FileInfo fileinfo = new FileInfo ("C: \ test.txt ");

Fileinfo. Delete ("D: \ test.txt ");


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.