C # Introduction to common file read/write classes,
First, you must be familiar with file and folder operations in. NET. File and Directory are the two most important classes. Understanding them will greatly facilitate the implementation of subsequent functions.
This section briefly introduces two. NET classes related to the file system.
The System. IO. File class and System. IO. FileInfo class provide various operations on files. The System. IO namespace must be referenced during use. The following describes the main attributes and methods of a program instance.
(1) File opening method: File. Open ()
The statement of this method is as follows:
Public static FileStream Open (string path, FileMode mode)
The following code opens the file named newfile.txt in the c: \ tempuploadsdirectory and writes hello to the file.
Private void OpenFile ()
{
FileStream. TextFile = File. Open (@ "c: \ tempuploads \ newFile.txt", FileMode. Append );
Byte [] Info = {(byte) 'h', (byte) 'E', (byte) 'l', (byte) 'l', (byte) 'o '};
TextFile. Write (Info, 0, Info. Length );
TextFile. Close ();
}
(2) File creation method: File. Create ()
The statement of this method is as follows:
Public static FileStream Create (string path ;)
The following code demonstrates how to create a file named newfile.txt under c: \ tempuploads.
Because File. by default, the Create method grants all users full read/write access to the new file. Therefore, the file is opened with the read/write access permission, which must be disabled before it can be opened by other applications. Therefore, you must use the Close method of the FileStream class to Close the created file.
Private void MakeFile ()
{
FileStream NewText = File. Create (@ "c: \ tempuploads \ newFile.txt ");
NewText. Close ();
}
(3) File deletion method: File. Delete ()
The method is declared as follows:
Public static void Delete (string path );
The following code deletes the newfile.txt file in the c: \ tempuploadsdirectory.
Private void DeleteFile ()
{
File. Delete (@ "c: \ tempuploads \ newFile.txt ");
}
(4) File Copy method: File. Copy
The method is declared as follows:
Public static void Copy (string sourceFileName, string destFileName, bool overwrite );
The following code copies c: \ tempuploads \ newFile.txt to c: \ tempuploads \ BackUp.txt.
The overwritegion of copetypes is set to true, so that if the backup.txt file already exists, it will be overwritten by the copied file.
Private void CopyFile ()
{
File. Copy (@ "c: \ tempuploads \ newFile.txt", @ "c: \ tempuploads \ BackUp.txt", true );
}
(5) File movement method: File. Move
The method is declared as follows:
Public static void Move (string sourceFileName, string destFileName );
The following code moves the backup.txt file under c: \ tempuploadsto the c root directory.
Note:
File Transfer can only be performed on the same logical disk. If you try to transfer files from drive C to drive D, an error will occur.
Private void MoveFile ()
{
File. Move (@ "c: \ tempuploads \ BackUp.txt", @ "c: \ BackUp.txt ");
}
(6) File Attribute setting method: File. SetAttributes
The method is declared as follows:
Public static void SetAttributes (string path, FileAttributes fileAttributes );
The following code sets the properties of file c: \ tempuploads \ newFile.txt to read-only and hidden.
Private void SetFile ()
{
File. SetAttributes (@ "c: \ tempuploads \ newFile.txt ",
FileAttributes. ReadOnly | FileAttributes. Hidden );
}
In addition to common read-only and hidden attributes, files include Archive, System, and Temporary. For details about file attributes, see the description of FileAttributes in MSDN.
(7) method for determining whether a File exists: File. Exist
The method is declared as follows:
Public static bool Exists (string path );
The following code checks whether the c: \ tempuploads \ newFile.txt file exists. If the file exists, copy the file first, delete it, and then move the copied file. If the file does not exist, create the file first, open the file, and write the file, finally, set the file attribute to read-only and hidden.
If (File. Exists (@ "c: \ tempuploads \ newFile.txt") // checks whether the File Exists.
{
CopyFile (); // copy a file
DeleteFile (); // delete an object
MoveFile (); // move the file
}
Else
{
MakeFile (); // generate a file
OpenFile (); // open the file
SetFile (); // set file attributes
}
In addition, the File class provides more support for Text.
· AppendText: append text to an existing file.
· CreateText: Creates or opens a new file for writing text.
· OpenText: open an existing text file for reading.
But the above method mainly on the UTF-8 encoding text operations, which is not flexible enough. We recommend that you use the following code to operate the txt file.
· Read the txt file. The sample code is as follows:
StreamReader TxtReader = new StreamReader (@ "c: \ tempuploads \ newFile.txt", System. Text. Encoding. Default );
String FileContent;
FileContent = TxtReader. ReadEnd ();
TxtReader. Close ();
· Perform the "write" Operation on the txt file. The sample code is as follows:
StreamWriter = new StreamWrite (@ "c: \ tempuploads \ newFile.txt", System. Text. Encoding. Default );
String FileContent;
TxtWriter. Write (FileContent );
TxtWriter. Close ();
System. IO. Directory and System. DirectoryInfo
It mainly provides various operations on directories. The System. IO namespace must be referenced during use. The following describes the main attributes and methods of a program instance.
(1) Directory Creation Method: Directory. CreateDirectory
The method is declared as follows:
Public static DirectoryInfo CreateDirectory (string path );
The following code creates a directory named NewDirectory in the c: \ tempuploads folder.
Private void MakeDirectory ()
{
Directory. CreateDirectory (@ "c: \ tempuploads \ NewDirectoty ");
}
(2) Directory attribute setting method: DirectoryInfo. Atttributes
The following code sets the c: \ tempuploads \ NewDirectory directory to read-only and hidden. The directory attribute is set with FileAttributes, which is the same as the file attribute.
Private void SetDirectory ()
{
DirectoryInfo NewDirInfo = new DirectoryInfo (@ "c: \ tempuploads \ NewDirectoty ");
NewDirInfo. Atttributes = FileAttributes. ReadOnly | FileAttributes. Hidden;
}
(3) Directory deletion method: Directory. Delete
The method is declared as follows:
Public static void Delete (string path, bool recursive );
The following code deletes the c: \ tempuploads \ BackUp Directory. The second parameter of the Delete method is of the bool type. It determines whether to Delete non-empty directories. If the value of this parameter is true, the entire directory will be deleted, even if there are files or subdirectories under the Directory; if it is false, it can be deleted only when the directory is empty.
Private void DeleteDirectory ()
{
Directory. Delete (@ "c: \ tempuploads \ BackUp", true );
}
(4) Directory movement method: Directory. Move
The method is declared as follows:
Public static void Move (string sourceDirName, string destDirName );
The following code moves the directory c: \ tempuploads \ NewDirectory to c: \ tempuploads \ BackUp.
Private void MoveDirectory ()
{
File. Move (@ "c: \ tempuploads \ NewDirectory", @ "c: \ tempuploads \ BackUp ");
}
(5) obtain all subdirectories in the current Directory: Directory. GetDirectories
The method is declared as follows:
Public static string [] GetDirectories (string path ;);
The following code reads all the subdirectories in the c: \ tempuploads \ directory and stores them in a string array.
Private void GetDirectory ()
{
String [] Directorys;
Directorys = Directory. GetDirectories (@ "c: \ tempuploads ");
}
(6) method for obtaining all files in the current Directory: Directory. GetFiles
The method is declared as follows:
Public static string [] GetFiles (string path ;);
The following code reads all the files in the c: \ tempuploads \ directory and stores them in a string array.
Private void GetFile ()
{
String [] Files;
Files = Directory. GetFiles (@ "c: \ tempuploads ",);
}
(7) method for determining whether a Directory exists: Directory. Exist
The method is declared as follows:
Public static bool Exists (
String path;
);
The following code checks whether the c: \ tempuploads \ NewDirectory directory exists. If yes, first obtain the subdirectories and files in the directory, move them, and then delete the moved directories. If the directory does not exist, create the directory first, and set the Directory attribute to read-only and hidden.
If (File. Exists (@ "c: \ tempuploads \ NewDirectory") // you can check whether a directory Exists.
{
GetDirectory (); // obtain sub-Directories
GetFile (); // get the object
MoveDirectory (); // move the Directory
DeleteDirectory (); // delete a directory
}
Else
{
MakeDirectory (); // generate a directory
SetDirectory (); // Set Directory attributes
}
Note:
There are three methods of path: relative path under the current directory, relative path of the current work disk, and absolute path. Take C: \ Tmp \ Book as an example (assuming the current working directory is C: \ Tmp ). "Book", "\ Tmp \ Book", and "C: \ Tmp \ Book" indicate C: \ Tmp \ Book.
In C #, "\" is a special character. To express it, use "\". This writing method is inconvenient, and the C # language provides @ to simplify it. You only need to add @ before the string to directly use "\". Therefore, the above path in C # should be represented as "Book", @ "\ Tmp \ Book", @ "C: \ Tmp \ Book ".
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].