I/O operation basics in. NET and. netio operation Basics
IO Introduction
For IO operations in. NET, you often need to call several classes.
1.FileStream class
File stream class, responsible for copying, reading and writing large files.
2.Path class
The methods in the Path class are basically string (File Name) operations, which have little to do with the actual file.
3.File class
The File class can be used to copy and cut small files and read some File files.
4.Dirctory
Directory operations, creating files, deleting directories, obtaining file names under directories, and so on.
Path class
1.ChangeExtension (path, ". jpg ")
Change the file suffix!
2.Combine (s1, s2)
Connect two paths
3.Several Methods for getting file names
1) Path. GetFileName (s1); // get the file name in the Path
2) Path. GetFileNameWithoutExtension (s1); // get the file name, excluding the suffix!
3) Path. GetDirectoryName (s1) // the directory in the obtained Path does not contain the file name.
4) Path. GetExtension (s1); // get the extension only
4. GetFullPath ("")
Obtain the complete path and obtain the absolute path based on the relative path.
5.Temporary directory
1) GetTempPath () // get the temporary directory of the current user
2) GetTempFileName () // obtain a random file name and create the file in the temporary directory.
3) GetRandomFileName () // obtain only one random file name.
Directory
1. Create a directory
Directory. CreateDirectory (@ "Directory ");
2. check whether a file exists in a directory.
String path = @ "path ";
If (Directory. Exists (path ))
{
}
3. delete a directory
1) Directory. Delete (path); // Delete an empty Directory with no files in it.
2) Directory. Delete (path, true); // Delete the Directory!
3) if there is no Directory, an exception will be reported. It is best to use if (Directory. Exists (path) to judge.
4.Move file directory
This is also used to rename the directory.
Directory. Move (@ "c: \ a", @ "c: \ abc ");
5.DirectoryInfo
You can use folders as objects.
DirectoryInfo dirInfo = new DirectoryInfo (@ "path ");
6.Obtain all direct sub-directories and direct sub-files in the current directory
// Obtain the direct subdirectories in the directory
String [] dirs = direcoies. GetDirectories (@ "directory ");
// Obtain the direct sub-files in the directory
String [] files = Directory. GetFiles (@ "Directory ");
7. Another method for obtaining directories and files under the current directory
// Obtain the direct subdirectories in the directory
// String [] dirs = direcow.getdirectories (@ "directory ");
// Obtain the direct sub-files in the directory
// String [] files = Directory. GetFiles (@ "Directory ");
// --------- The above is the practice of 6, which has certain performance problems. What's the problem? -------------
Use String [] dirs = direcoies. GetDirectories (@ "directory"); the set can be traversed only after the entire string is obtained.
If you want to, read one to process one.
We recommend that you use Directory. EnumerateFile ()
1) EnumerateFile returns a set of IEnumerable interfaces. In fact, the iterator mode is used.
2) The SearchOption. ALLDirctories parameter allows. EnumerateFile () to traverse files in all subdirectories.
File class
1. Copy
File. Copy ("Source", "Target", true );
2.Determine whether there is
File. Exists (@ "Source ");
3.Cut
File. Move ("Source", "Targe ");
4.Create
File. Create ("path ");
5.Delete
File. Delete ("path"); // Delete. If no, no error is returned!
6.Read operations
1) File. ReadAllLines ("path", Encoding. Default); // returns a string []
2) File. ReadAllTest ("path", Encoding. Default); // string
3) File. ReadAllBytes ("path ");
7.Write operation
1) File. WriteAllLines ("path", new string [4], Encoding. Default); // write data to a File by row.
2) File. WriteAllText ("path", "string ");
3) File. WriteAllBytes ("path", new byte [4]);
4) File. AppendAllText (); // append string to the File.
8. Return the FileStream shortcut
1) File. Open (string, FileMode); // returns a FileStream
2) File. OpenRead (string, FileMode); // returns a read-only FileStream.
3) File. OpenWrite (string, FileMode); // return a write-only FileStream.