WinForm dialog box, stream, winform dialog box
Private void button1_Click (object sender, EventArgs e) {// display color selector colorDialog1.ShowDialog (); // assign the obtained color to panel panel1.BackColor = colorDialog1.Color ;}
FontDialog: Font Style selection Control
Private void button3_Click (object sender, EventArgs e) {// display fontDialog1.ShowDialog (); // value: label2.Font = fontDialog1.Font ;}
FolderBrowserDialog: folder Selection Control
Private void button2_Click (object sender, EventArgs e) {// display folder selector folderBrowserDialog1.ShowDialog (); // obtain the file path label1.Text = folderBrowserDialog1.SelectedPath ;}
OpenFileDialog: file selection Control
※Restrict the suffix of open files: Filter = text files | *. txt | all files | *.*;
One suffix is displayed as one group, and the other group needs to be added'|'
Private void button4_Click (object sender, EventArgs e) {// sets the type of file that can be opened. openFileDialog1.Filter = "text file | *. txt | all files | *. * "; // display DialogResult: obtain or set the dialog box result DialogResult dr = openFileDialog1.ShowDialog (); // set the value if (dr = DialogResult. OK) // to Prevent Users From selecting {label3.Text = openFileDialog1.FileName ;}}
SaveFileDialog: Specifies the Save Path Selection control.
※Existing files are overwritten.
Private void button5_Click (object sender, EventArgs e) {// display DialogResult dr = convert (); // value: if (dr = DialogResult. OK) {label4.Text = saveFileDialog1.FileName ;}}
Ii. Stream File Operations
Stream:(I/O) input/output stream
Category: file stream, memory stream, and network stream
Stream operations should generally be placed in try catch, and operations on the file network are prone to exceptions.
Namespace:Using system. IO;
Using system. Text; // namespace required for Binary Conversion
(1) Class: FileStream: file stream
1. Construct: Generally, the third and sixth reloads are used.
FileStream stream = new FileStream ("d: \ test.txt" (PATH), FileMode. Creat (open mode), FileAccess. Read (Read-only ));
※Add @ in front of the write path @"D: \ test.txt", Or write a double slash.
The second Parameter Enumeration type
CreateNewThe specified operating system should create a new file. If the file exists, an exception is thrown.
CreateSpecify the operating system to create a new file. If the file already exists, overwrite it.
OPenSpecify the operating system to open an existing file. If the file does not exist, an exception is thrown.
OpenOrCreateSpecify the operating system to open the file. If the file does not exist, create it.
TruncateSpecify the operating system to open an existing file. If the file already exists, it will be cleared. reading from the file opened by Truncate will cause an exception.
AppendIf the file exists, locate the file and find the end of the file, or create a new file.
The third parameter determines the file access method, which is readable and writable by default.
Read-OnlyRead access to a file with the read permission.
Write-onlyWrite access to files with write permission.
ReadWrite readable and writableThe read and write access to files has the read and write permissions.
2. attributes:
Length: Stream Length
Position: The current position of the stream. It is used to detect the current position of the cursor.
3. Method:
Write(Byte [] stream content, where int is written, and int Is written );
Parameter, the first binary array, and the second position from which the write generally starts from 0, and the third write in Length (generally use Length, which must be converted to int ).
Read (Byte [] stores the space for reading the stream, where the int reads from, and how long the int reads); read the file
Seek(Int offset, SeekOrigin. Begin (from which offset is calculated) to adjust the current position of the stream, seek (0, seekOrigin. End), move the cursor to the End
Flush ();Clear Cache
Close ();Close stream
4. Usage:
Read File Content:
Private void button#click (object sender, EventArgs e) {// display tool DialogResult dr = openFileDialog1.ShowDialog (); // value: if (dr = DialogResult. OK) {// file path string path = openFileDialog1.FileName; // create a file stream FileStream fs = new FileStream (path, FileMode. open); // create a binary array to store read Data byte [] sj = new byte [fs. length]; // read the file as binary data and put it in the binary array fs. read (sj, 0, sj. length); // convert binary data to the string Encoding format. Default automatically obtains the Encoding method of the current program. Getstring returns the string richTextBox1.Text = Encoding. default. getString (sj); // close the stream fs. close ();}}
Write File: store the written content
Private void button2_Click (object sender, EventArgs e) {// display the Select File Dialog Box DialogResult dr = saveFileDialog1.ShowDialog (); // obtain the path if (dr = DialogResult. OK) {// file path string path = saveFileDialog1.FileName; // retrieve the content in the text box string nr = richTextBox1.Text; // convert the string into a binary array byte [] sj = Encoding. default. getBytes (nr); // create a file stream FileStream fs = new FileStream (path, FileMode. create); // write data to the file fs. write (sj, 0, sj. length); // close the stream fs. close ();}}
(2) StreamWrite class: Write only
Private void button3_Click (object sender, EventArgs e) {// display the Select File Dialog Box DialogResult dr = saveFileDialog1.ShowDialog (); // obtain the path if (dr = DialogResult. OK) {// file path string path = saveFileDialog1.FileName; // retrieve the content in the text box string nr = richTextBox1.Text; // create a file stream FileStream fs = new FileStream (path, FileMode. create); // Create a StreamWrite object StreamWriter sw = new StreamWriter (fs, Encoding. default); // write the sw file. write (nr); // close sw. close (); fs. close ();}}
First, use the stream
StreamWriter sw = new StreamWriter (fs, Encoding. Default );
The second method does not use the second parameter of the stream to determine whether to Append content.
StreamWriter sw = new StreamWriter (path, true, Encoding. Default );
(3) StreamRead class: Read-Only
// Obtain the file path
String path = saveFileDialog1.FileName;
// Open the file stream
FileStream fs = new FileStream (path, FileMode. Create, FileAccess. Write );
// The first method is to use the stream
StreamReader sr = new StreamReader (fs, Encoding. Default );
// Method 2, not applicable to stream
// StreamReader sr = new StreamReader (path, Encoding. Default );
// Read a row
RichTextBox1.Text = sr. ReadLine ();
// Read all
RichTextBox1.Text = sr. ReadToEnd ();
// Close
Sr. Close ();