Summary of the basic methods for. Net File Operations

Source: Internet
Author: User

First look at the sample code snippet:
1. Read files:
Filestream usage
System. Io. filestream FS = new filestream ("test.txt", filemode. openorcreate, fileaccess. Read );
Char [] cs = new char [fs. Length];
Byte [] BS = new byte [fs. Length];
FS. Read (BS, 0, (INT) fs. Length );
Encoding E = system. Text. encoding. utf8;
Cs = E. getchars (BS );

Foreach (char C in CS)
{
Console. writeline (C );
}
Streamreader usage:
String STR;
Using (streamreader sr = new streamreader ("test.txt "))
{
While (STR = Sr. Readline ())! = NULL)
{
Console. writeline (STR );
}
}
2. Write files:
Filestream usage:
System. Io. filestream FS = new filestream ("test.txt", filemode. truncate );
String STR = "Hello, this is my CC ";
Byte [] array = new byte [Str. Length];
System. Text. Encoding E = system. Text. utf8encoding. utf8;
Array = E. getbytes (STR );
FS. Write (array, 0, array. Length );
Streamwrite usage:
Using (streamwriter Sw = new streamwriter ("test.txt", true, encoding. getencoding ("gb2312 ")))
{
Sw. writeline ("hello, I miss you ");
}
The following is a summary:
From the instance code above, we can see the strings for streamreader and streamwrite operations, while filestream operates on Byte arrays. Byte data must be converted into byte arrays and character arrays through the encoding class. This is the essential difference between them.
In addition, the usage of classes in the system. Io namespace is supplemented:
The system. Io namespace contains the main classes used for file input and output.
File: provides many static methods for moving, copying, and deleting files.
Directory: provides many static methods for moving, copying, and deleting directories.
Path: processing class, used to process the path name.
Fileinfo: indicates the physical file on the disk. It can be used to process the file. To read and write files, you must create a stream object.
Directoryinfo: indicates the physical directory on the disk, which can be processed.
Filestream: a file that can be written, read, or both. This file can be read and written synchronously or asynchronously.
Streamreader: reads character data from a stream and can be created as the base class by using filestream.
Streamwriter: Write character data to a stream. You can use filestream to create a base class.
Filesystemwatcher: used to monitor files and directories and give events that can be captured by applications when these locations change.

File:

Method
Description
 
Copy ()
Copy the file to the specified location
 
Create ()
Create a file in the specified path
 
Delete ()
Delete an object
 
Open ()
Returns the filestream object in the specified path.
 
Move ()
Move the specified file to a new location. You can specify different names for the file in the new location.
 

Some common static methods of directory:

Method
Description
 
Createdirectory ()
Create a directory with a specified path
 
Delete ()
Delete the specified directory and all the files in it
 
Getdirectories ()
Returns an array of directory objects under the current directory.
 
Getfiles ()
Returns the array of file objects in the current directory.
 
Move ()
Move the specified directory to a new location. You can specify a new name for the folder in the new location.

Fileinfo class:

It has no static method and can only be used for instantiated objects. The fileinfo object indicates the file in the disk or network location. Note that it is not a stream. To read or write files, you must create a stream object.

If you only call a single method on an object, you can use the static file class. Here, static calling is faster, because. NET Framework does not have to traverse instances to create new objects and call methods. However, if an application performs several operations on a file, it is better to instantiate the fileinfo object and use the method. This saves time, because the object will reference the correct file on the file system, and the static class must search for the file every time.

The Basic Attributes provided by the fileinfo class can be used to update files.

Attribute
Description
 
Attributes
Obtains or sets the attributes of the current file.
 
Creationtime
Obtains the creation date and time of the current file.
 
Directoryname
Path of the returned file directory
 
Exists
Determine whether a file exists
 
Fullname
Retrieve the complete path of a file
 
Length
Get the file capacity
 
Name
Only the file name is returned, not the complete file path.
 

 

Directoryinfo class:

The directoryinfo class is similar to the fileinfo class. It is an instantiated object that represents a single directory on the computer. Similar to the fileinfo class, many method calls can be copied between directory and directoryinfo. When both are used, the same principle can be used: static directory classes are used for a single call. If a series of calls are performed, the instantiated directoryinfo object is used.

The directoryinfo class shares almost all the same attributes with the fileinfo class, except that the directoryinfo class works in a directory without files.

 

Filestream object:

Indicates the stream that points to the file on the disk or network path. When the class provides methods for reading and writing bytes to files, streamreader and streamwriter are often used to perform these functions. This is because the filestream class operates on Byte and byte arrays, while the stream class operates on character data.

There are also several ways to create a filestream object. Constructor has many different overload/versions, but the simplest constructor only has two parameters: File Name and filemode enumeration.

Filestream afile = new filestream ("a.txt", filemode. openorcreate );

Filemode enumerated members:

Filemode enumeration member
Description
 
Append
If the file exists, open the file, move the file location to the end of the file, and create a new file. Filemode. append can only be used with enumeration fileaccess. Write
 
Create
Create a new file. If such a file exists, destroy it.
 
Createnew
Create a new file. If the file already exists, an exception is thrown.
 
Open
Open an existing file. If the specified file does not exist, an exception is thrown.
 
Openorcreate
If the file exists, open the file; otherwise, create a new file. If the file already exists, the data in the file is retained.
 
Truncate
Open an existing file and clear its content. Then we can write new data to the file, but keep the initial creation date of the file. There must be a file; otherwise, an exception will be thrown.
 

The previous constructor opens the file in read-only mode by default. An additional parameter is required to specify different access levels. This parameter is the fileaccess parameter.

Filestream afile = new filestream ("a.txt", filemode. openorcreate, fileaccess. Write );

This line of code will open the file and write and access the file. Any attempt to read the file will throw an exception. There are only three types of fileaccess enumeration: read, readwirte, and write. You can open a file, read-only, write-only, or read/write operations. This attribute changes the user's access to files based on the user's authentication level.

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.