C # file IO operations

Source: Internet
Author: User

I. Overview of file systems
One of the important functions of computer systems is to quickly process a large amount of information. Therefore, data organization and access have become an extremely important part. Files are an organizational form of information, and the goal of the file system is to improve the memory utilization and accept user-initiated operations on files.
A file system is an important part of an operating system. The problems to be solved by the file system include: Managing storage settings, determining the storage location and method of files, providing the sharing capability, ensuring file security, and providing friendly user interfaces. Through the file system, users and applications can easily store data without worrying about the implementation of underlying storage devices.
Windows supports multiple file systems, such as FAT, FAT32, and NTFS. These file systems have different implementation methods within the operating system, but the interfaces they provide to users are consistent. If the application does not involve the specific features of the operating system, you only need to write the code according to the standard, and the generated application can run on each file system, it can even be transplanted to other operating systems (such as Unix and Linux) without modification. the IO Processing Section in the. NET Framework encapsulates the implementation details of the file system and provides developers with a standardized interface.
Windows uses a multi-level directory structure for the file system and provides a set of commands for file and directory management. You can use the standard methods provided by. NET for directory management, file control, and file access. When a program is compiled and executed on behalf of friends, the. NET Framework automatically calls related system commands.
C # Treat a file as a byte sequence and operate the file in the form of a stream. Stream is the concept of creating byte sequences. Files, input/output designs, internal program pipelines, and TCP/IP sockets can all be considered as a stream .. The NET concept of convection is abstracted to provide a unified view for these different types of input and output, so that programmers do not have to understand the specific details of the operating system and basic equipment.
Ii. directories and files
1. enumeration types
A) FileAccess. This enumeration type indicates the object access permission, which can be the following values.
I. Read --- Read the object
Ii. ReadWrite --- read and write permissions on files
Iii. Write --- Write permission on the file
B) FileAttributes. This enumeration type indicates the file type.
I. Archive --- Archive file
Ii. Compressed --- Compressed file
Iii. Device --- Device File
Iv. Directory --- Directory
V. Encrypted --- encrypt the file
Vi. Hidden --- hide a file
Vii. Normal --- common file
Viii. NOtContentIndexd --- no index file
Ix. Offline --- Offline files
X. ReadOnly --- read-only file
Xi. ReparsePoint --- re-analysis File
Xii. SparseFile --- sparse file
Xiii. System --- System File
Xiv. Temporary --- Temporary file
Enumeration values can be combined by bit. For example, FileAttributes. System | FileAttributes. ReadOnly indicates the System read-only file. Of course, some mutually exclusive types cannot be combined. For example, a file cannot be both a common file and a hidden file.
C) FileMode. This enumeration type indicates the file opening mode. It can be set to the following values:
I. Append --- open the file in Append mode. If the file exists, move it to the end of the file; otherwise, create a new file.
Ii. Create --- Create and open a file. If the file already exists, it overwrites the old one.
Iii. Open --- Open an existing file. If the file does not exist, an exception occurs.
Iv. OpenOrCreate --- open or create a new file. If the file exists, open it. Otherwise, create and open a new file.
V. Truncate --- open an existing file and clear the file content.
D) FileShare. This enumeration type indicates the file sharing mode, which can be the following values.
I. None --- prohibit any form of sharing.
Ii. Read --- Read sharing. After opening the file, other processes are allowed to Read and write the file.
Iii. ReadWrite --- read/write sharing: open the file and allow other processes to read and write the file.
Iv. Write --- Write share. open the file and allow other processes to Write the file.
E) SeekOrigin. This enumeration type indicates the object offset. It can be the following values:
I. Begin --- start position of the file.
Ii. Current --- count from the Current position of the file stream.
Iii. End --- It is counted from the End of the file stream.
F) policyfilters. This enumeration type is used to specify which attributes of a file or directory are modified for monitoring. It can be set to the following values:
I. Attributes --- monitors attribute changes.
Ii. CreationTime --- monitor changes in the creation time.
Iii. DirectoryName --- monitors directory name changes.
Iv. FileName --- monitors file name changes.
V. LastAccess --- monitors changes in the last access time.
Vi. LastWrite --- monitors the last time change.
Vii. Security --- monitors changes in Security settings.
Viii. Size --- monitors changes in the Size.
2. Directory
Using the Directory management function provided by the Directory class, you can not only create, move, and delete directories, but also obtain and Set Directory information.
Directory provides the following static methods:
Public static DirectoryInfo CreateDirectory (string) --- specify the path name, create a directory, and return the directory information.
Public static void Delete (string) --- specify the path name and Delete the directory.
Public static void bool Exists (string) ---- specifies the path name and determines whether the directory Exists.
Public static DateTime GetCreationTime (string) ---- get the Directory creation date and time based on the given path name.
Public static string GetCurrentDirectory () --- get the current working directory of the application.
Public static string [] GetDirectory (string) --- get the list of subdirectories in the directory given the path name.
Public static string GetDirectoryRoot (string) --- obtain the volume information or required information of the directory based on the given path name.
Public static string [] GetFile (string) --- get the file list in the directory given the path name.
Public static string [] GetFileSystemEntries (string) --- get the subdirectory and file list in the directory given the path name.
Public static DateTime GetLastAccessTime (string) --- get the date and time of the last file access given the path name.
Public static DateTime GetlastWriteTime (string) --- specifies the path name to obtain the date and time of the last modification to the directory.
Public static string [] GetLogicalDrivers () --- obtain the list of logical drives on the computer.
Public static DirectoryInfo GetParent (string) --- give the path name and obtain the information of the upper directory.
Public static void Move (string, string) --- specify the Source Path Name and target path name to Move the directory.
Public static void SetCreationTime (string, DateTime) --- specify the path name and set the Directory creation date and time.
Public static void SetCreationDirectory (string) --- specify the path name and set the directory to the current working directory of the application.
Public static void SetLastAccessTime (string, DateTime) --- specify the path name and set the date and time of the last access to the directory.
Public static void SetLastWriteTime (string, DateTime) --- specify the path name and set the last modification date and time of the directory.

3. File
A) basic file operations
Public static FileStream Create (string) --- specifies the file path name, creates a file, and returns a FileStream Stream object.
Public static StreamWriter CreateText (string) --- specifies the file path name, creates a file in text format, and returns a StreamWriter Stream object.
Public static void Copy (string, string) --- specifies the Source Path Name and directory path name, and specifies the Beibei file.
Public static void Move (string, string) --- specifies the Source Path Name and directory path name to remove the files.
Public static void Delete (string) --- specify the Source Path Name and directory path name and Delete the file.
Public static void Exists (string) --- specifies the Source Path Name and directory path name to determine whether the file Exists.

B) Obtain and set file information
Public static FileAttributes GetAttributes (string) ------ get the attribute set of the file given the Source Path Name.
Public static DateTime GetCreationTime (string) --- get the date and time of object creation based on the given file path name.
Public static DateTime GetLastAccessTime (string) --- given the file path name, obtain the date and time when the file was last accessed.
Public static DateTime GetLastWriteTime (string) --- get the Last modified Date and time of the file given the file path name.
Public static void SetAttributes (string, FileAttributes) --- specify the file path name and set the File Attribute Set.
Public static void SetCreationTime (string, DateTime) --- specify the file path name and set the date and time of file creation.
Public static void SetLastAccessTime (string, DateTime) ---- specify the file path name and set the date and time when the file was last accessed.
Public static void SetLastWriteTime (string, DateTime) --- specify the file path name and set the Last modified Date and time of the file.

C) open the file and associate the file with the stream object
Public static FileStream Open (string, FileMode) --- specify the file path name, Open the file in the specified way, and return a FileStream Stream object.
Public static FileStream OpenRead (string) --- specifies the file path name, opens the file in read-only mode, and returns a FileStream Stream object.
Public static FileStream OpenWrite (string) --- specify the file path name, open the file for read/write operations, and return a FileStream Stream object.
Public static StreamReader OpenText (string) --- open the file in text mode and return a FileStream Stream object given the file path name.
Public static StreamWriter AppendText (string) --- specify the file path name, open the file in text mode for write operations, and return a StreamWriter Stream object.

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.