Provides static methods for creating, copying, deleting, moving, and opening a single file and assists in creatingFileStreamObject.
1. Read the file:
1. Return string:
string readText = (@"c:\temp\");
2. Return string array:
string[] readText = (@"c:\temp\", Encoding.UTF8);
3. Return byte array:
byte[] buffer = (@"c:\temp\"); string str = (buffer, 0, );
4. Return to StreamReader
Open existing UTF-8 text for reading
using (StreamReader sr = (@"c:\temp\")) { string s; while ((s = ()) != null) { (s); } }
2. Write a document
Create a new file to write contents to it, and overwrite the file already exists.
1. Write string:
string createText = "Hello and Welcome" + ; (path, createText);//(),()
2. Write to the string array:
string[] createText = { "Hello", "And", "Welcome" }; (path, createText);
3. Write to byte array:
string str = "Hahahahahahahaha"; byte[] buffer = (str); (path,buffer);
4. Return to StreamWriter
Create or open existing UTF-8 text for writing or appending
using (StreamWriter sw = (path)) //StreamWriter:()、() { ("Hello"); ("And"); ("Welcome"); }
3. Return to FileStream operation
(): Default is not shared and has read/write access permissions
using (FileStream fs = (path, , , )) { byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while ((b, 0, ) > 0) { ((b)); } }
(): Read access permission
slightly
: Write access permission
using (FileStream fs = (path)) { Byte[] info = new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method."); // Add some information to the file. (info, 0, ); }
():
using (FileStream fs = (path)) { Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file."); // Add some information to the file. (info, 0, ); }
4. Common operations of File class:
- File deletion method:()
- File copy method:()
- File moving method:()
- Set file attribute method:/Get***()
- How to determine whether a file exists:()
5. Common operations of Directory class:
//Delete this directory(@"C:\New folder") //Delete this directory, true means that if this directory has a subdirectory, it will also be deleted, otherwise an exception will be thrown(@"C:\New folder", false); //Is this directory existsbool b = (@"C:\New folder"); //Return to the subdirectory in this directory according to the pathstring[] dirs1 = (@"C:\New folder"); //The second parameter indicates: the search range is that the search folder contains the "basic" keywordstring[] dirs2 = (@"C:\New folder", "Base"); //Search all files in the directorystring[] files = (@"C:\New folder"); //The third parameter indicates: specify whether the search operation should include all subdirectories or only the current directory.string[] files1 = (@"C:\New folder", "", ); // (Return a DirectoryInfo array when obtaining all directories in the specified directory.)DirectoryInfo dirs = (@"C:\New folder"); //Move, cut. Only on the same disk. There is no Copy method for the directory. Rename can be implemented using the Move() method.(@"F:\Test\33", @"F:\test\32\33");
6. FileSystemInfo
Derived classes:
- DirectoryInfo
- FileInfo
1. FileInfo class
//Instantiate FileInfo to operateFileInfo myfile = new FileInfo(path); //Declare an object to operate on a file(destpath); //Copy the file, the copy path is destpath(destpath); //Perform mobile operations(); //Delivery //Get detailed information of a certain file or folder (creation date, last modified date, etc.)FileInfo myfile = new FileInfo(path); //Declare an object to operate on a fileDateTime dt = ; //Get or set the date of creation of the file/folderstring filepath = ; //It can only be used for FileInfo, obtain the complete path name, path + file namebool file = ; //The value of this property indicates whether the file or folder exists. If it exists, it will return Truestring fullname = ; //Get the full path name of the file or folderDateTime lastTime = ; //Get or set the time to access the file or folder for the last timeDateTime lastWrite = ; //Get or set the time to modify a folder or folder for the last timestring name = ; //Get the file name, it cannot be modifiedlong length = ; //Return the byte size of the file//CreationTime,LastAccessTime,LastWriteTimeAll can be modified。
2. DirectoryInfo class
DirectoryInfo dir = new DirectoryInfo(@"d:\C#Programming");if (!) { (); } else { ("This directory already exists"); }
7. DriveInfo class
In Windows operating systems, storage media is collectively called drives. Since the hard disk can be divided into multiple areas, each area is called a drive.
Common field members of DriveInfo class include
- DriveFormat (file system format, such as NTFS or FAT32),
- DriveType (drive type),
- Name (drive name),
- TotalSize (total space),
- TotalFreeSpace (gets free space for drive).
Commonly used method members are GetDrives (get list of available drives).
The enumeration values of the DriveType enumeration type include CDRom (optical drive), Fixed (hard drive), Network (network drive), Removeable (floppy disk or USB drive), etc.
For example, the following code can output the remaining space information for each hard drive.
DriveInfo[] drivers = (); foreach (DriveInfo driver in drivers) { if ( == && == "NTFS") { ("exist{0}There is also a drive{1}Remaining space of bytes。", , ); } }
This is all about this article about C# using File static class to read and write files. I hope it will be helpful to everyone's learning and I hope everyone will support me more.