First, dialog box
ColorDialog : Color selection Controls
private void Button1_Click (object sender, EventArgs e) { //Display Color selector colordialog1.showdialog (); Assigns the color to the panel Panel1. BackColor = colorDialog1.Color; }
FontDialog: Font Style selection control
private void Button3_Click (object sender, EventArgs e) { //Display font style selector fontdialog1.showdialog (); Value Label2. Font = Fontdialog1.font; }
FolderBrowserDialog: Folder Selection control
private void Button2_Click (object sender, EventArgs e) { //Display folder selector folderbrowserdialog1.showdialog (); Fetch the file path Label1. Text = Folderbrowserdialog1.selectedpath; }
OpenFileDialog: File Selection control
※ Restrict open file suffix: Filter = text file |*.txt| all files |*.*;
One shows a suffix is a group, adding another group needs to continue using '|'
private void Button4_Click (object sender, EventArgs e) { //sets what type of file can be opened openfiledialog1.filter = "Text File |* . txt| all Files |*.* "; Show DialogResult: Gets or sets the dialog box result of the form DialogResult dr = Openfiledialog1.showdialog (); Value if (dr = = DialogResult.OK)//In order to prevent the user from not selecting { label3. Text = Openfiledialog1.filename; } }
SaveFileDialog: Save path selection control
※ Selecting an existing file will overwrite
private void Button5_click (object sender, EventArgs e) { //display DialogResult dr = Savefiledialog1.showdialog (); Take the value if (dr = = DialogResult.OK) { label4. Text = Savefiledialog1.filename; } }
Second, the flow file operation
Flow:(I/O) input/output stream
Categories: file streams, memory streams, network streams
The operation of the flow is generally placed in the try catch, the operation of the file network prone to anomalies
namespaces:using System. IO;
Using System. Text; namespaces required for binary conversions
(i) Class: FileStream: File stream
1. Construction : General use of third and sixth overloads
FileStream stream = new FileStream ("D:\\test.txt" (Path), Filemode.creat (open mode), FileAccess.Read (read-only));
※ When writing the path, add @"D:\test.txt"or write double slash
Second parameter enumeration type
CreateNew Specifies that the operating system should create a new file and throw an exception if the file exists.
Create specifies that the operating system creates a new file and overwrites it if the file already exists.
Open Specifies that the operating system should open an existing file and throw an exception if the file does not exist.
OpenOrCreate Specifies that the operating system should open the file and create it if the file does not exist.
Truncate Specifies that the operating system opens an existing file, and if the file already exists, it is emptied, and reading from the Truncate open file throws an exception.
Append If the file exists, locate the file and find the end of the file, or create a new file.
The third parameter determines how the file is accessed by default readable writable
Reads read- only access to the file, with Read permission.
Write only writes write access to the file, with write permission.
ReadWrite Read and write access to a file, with read and write two permissions.
2. Properties:
Length: The duration of the stream
Position: Current position of the stream, probing the current position of the cursor
3. Method:
Write (byte[] The contents of the stream, int writes from the first position, int writes the length);
Arguments, the first binary array, and the second from which to start writing generally starting from 0, the third one to write in how long (generally with length, need to be strong to int).
Read (byte[] stores the space of the readout stream, int reads from the first position, int reads how long); Read the file
seek(int offset, seekorigin.begin (from which to calculate offset)) Adjusts the current position of the stream, Seek (0,seekorigin.end), moves the cursor to the end
Flush (); Clear Cache
Close (); Close the stream
4. Usage:
Read the contents of the file:
private void Button1_Click (object sender, EventArgs e) { //Display tool DialogResult dr = Openfiledialog1.showdialog (); Value if (dr = = DialogResult.OK) { //file path string path = Openfiledialog1.filename; Create file stream FileStream fs = new FileStream (path,filemode.open); Create a binary array to store the read Data byte[] sj = new BYTE[FS. Length]; Read the file as binary data, and put it in the binary array inside FS. Read (SJ,0,SJ. Length); Convert binary data to string Encoding format conversion Default automatically gets the encoding of the current program GetString returns the string richTextBox1.Text = Encoding.Default.GetString ( SJ); Close Stream fs. Close (); } }
Write a file: Deposit the contents of the writing
private void Button2_Click (object sender, EventArgs e) { //Display Select File dialog box DialogResult dr = Savefiledialog1.showdialog (); Fetch path if (dr = = DialogResult.OK) { //file path string path = Savefiledialog1.filename; Remove the contents of the text box string nr = richTextBox1.Text; Convert string to binary array byte[] sj = Encoding.Default.GetBytes (NR); Create a file stream FileStream fs = new FileStream (path,filemode.create); Write data fs to the file . Write (SJ,0,SJ. Length); Close Stream fs. Close (); } }
(ii) Streamwrite Category: can only write
private void Button3_Click (object sender, EventArgs e) { //Display Select File dialog box DialogResult dr = Savefiledialog1.showdialog (); Fetch path if (dr = = DialogResult.OK) { //file path string path = Savefiledialog1.filename; Remove the contents of the text box string nr = richtextbox1.text;//Create a file stream FileStream fs = new FileStream (path, filemode.create); Build a Streamwrite object StreamWriter sw = new StreamWriter (FS, encoding.default); Write to file SW. Write (NR); Turn off SW. Close (); Fs. Close (); } }
The first way, using a stream
StreamWriter SW = new StreamWriter (Fs,encoding.default);
The second way, do not use the stream the second argument is whether to append the content
StreamWriter SW = new StreamWriter (Path,true,encoding.default);
(iii) Streamread class: Read Only
Fetch file path
string path = Savefiledialog1.filename;
Open File stream
FileStream fs = new FileStream (path,filemode.create,fileaccess.write);
The first way, using a stream
StreamReader sr = new StreamReader (Fs,encoding.default);
The second way, the flow is not applicable
StreamReader sr = new StreamReader (Path,encoding.default);
Read a line
richTextBox1.Text = Sr. ReadLine ();
Read all
richTextBox1.Text = Sr. ReadToEnd ();
Shut down
Sr. Close ();
C # Form WinForm dialog box, flow