File and folder operations:
First, Flow:
File stream: FileStream. Used to manipulate files.
Namespaces: System.IO;
Steps to use the flow:-The operation of the most native stream. --the most versatile.
1. Create a stream.
FileStream fs = new FileStream ("File path", FileMode enumeration);
Filemode.appen-if present, open append, if not existing, create new
FileMode.Create-if present, overwrite. New if it does not exist.
FileMode.OpenOrCreate. If it exists, open it, and the new is not there.
2. Read or write to the stream.
(1).. Write stream:
The first step: you need to turn the string into a binary array:
Byte[] AAA = System.Text.Encoding.Default.GetBytes (txt. Text);
Step two: Write the stream
Fs. Write (AAA, 0, AAA. Length);
The first parameter: the binary data to be written in;
The second parameter: The starting position is generally 0
The third parameter: the length to write to the stream. is usually the length of the array
(2). Read stream:
The first step: Prepare a binary array to receive binary data.
Byte[] aaa = new Byte[fs. Length]; The length of the array is typically defined by the length of the stream, because this array is used to store the data in the stream.
The second step: use to read, put the read out in the above binary array.
Fs. Read (aaa,0,aaa. Length);
First parameter: Binary data that is read out;
The second parameter: The starting position is generally 0. From where to start reading
The third parameter: the length to read. is usually the length of the array. That is, the length of the stream
Step three: Turn the read-out binary array into a string.
string s = System.Text.Encoding.Default.GetString (AAA);
3. Close the stream.
Fs. Close ();
How to prevent memory leaks when working with files:
Law One: try...finally
Method two: using () {}
Case:
private void Btnsave_click (object sender, EventArgs e)
{
FileStream fs = null;
Try
{
Create a Stream
FS = new FileStream ("Aaa.txt", FileMode.Create);
Turn the string of the text box into 10,101,012 binary byte[]
Byte[] AAA = System.Text.Encoding.Default.GetBytes (txt. Text);
Write stream
Fs. Write (AAA, 0, AAA. Length);
}
Finally
{
Close the stream
if (fs! = NULL)
{
Fs. Close ();
}
}
}
private void Button1_Click (object sender, EventArgs e)
{
Create a Stream
using (FileStream fs = new FileStream ("Aaa.txt", FileMode.Open))
{
Prepares an empty binary array to receive read-out content
Byte[] aaa = new Byte[fs. Length];
Read stream
Fs. Read (AAA, 0, AAA. Length);
Convert binary into string
Txt. Text = System.Text.Encoding.Default.GetString (AAA);
Close the stream
Fs. Close ();
}
}
For file streams in the form of text (strings), you can use StreamWriter and StreamReader to simplify operations.
1. Create a Stream
FileStream fs = new FileStream ("Bbb.txt", FileMode.Create);
FileStream fs = new FileStream ("Bbb.txt", FileMode.Open);
2. Set reader or writer.
StreamWriter writer = new StreamWriter (FS);
StreamReader reader = new StreamReader (FS);
3. Use the reader or writer to manipulate the file stream.
Writer. Writer ("string");
string s = Reader. ReadToEnd ();
4. Close the reader and file stream.
Writer. Close ();
Reader. Close ();
Fs. Close ();
Files in the form of text (strings) can also be simplified. --No write stream creation, shutdown, direct use of stream reader and writer
private void Button4_Click (object sender, EventArgs e)
{
StreamWriter writer = new StreamWriter ("Ccc.txt", true, Encoding.default);
Writer. Write (txt. Text);
Writer. Close ();
}
private void Button5_click (object sender, EventArgs e)
{
StreamReader reader = new StreamReader ("Ccc.txt", Encoding.default);
Txt. Text = reader. ReadToEnd ();
Reader. Close ();
}
Dynamic generation and operation of controls:
Event three elements: Event source, event data, event handler
sender-Event Source, eventargs-event data, function body is the handler
private void Button1_Click (object sender, EventArgs e)
{
Button BTN = sender as Button; Image the source of the event.
MessageBox.Show (btn. Text); Displays the text of the event source.
}
Loads the data in the database and, if there is a lot of content, displays the scroll bar. Similar to the effect of the QQ friends list.
1. Put a panel on the dock layout in the form.
2. In this panel, put a FlowLayoutPanel, set it from top to dirty layout.
3. The outside panel needs to be set autoscroll=true. The scroll bar is displayed when out of range. **
4. Inside the FlowLayoutPanel, set it dock=top,autosize=true
Windows Form-----Content (12) Streams and events