C # Summary of file writing methods

Source: Internet
Author: User

File Processing must first import the namespace: using System.IO;

// Implementation Background: a text box, a button, and VS2005

Method 1: Use FileStream

// Instantiate a save file dialog box
SaveFileDialog sf = new SaveFileDialog ();
// Set the file storage type
Sf. Filter = "txt file | *. txt | all files | *.*";
// If the user does not enter the extension, the suffix is automatically appended.
Sf. AddExtension = true;
// Set the title
Sf. Title ="Write files";
// If the user clicks the Save button
If (SF. showdialog () = dialogresult. OK)
{
// Instantiate a file stream ---> associated with the written file
Filestream FS = new filestream (SF. filename, filemode. Create );
// Obtain the byteArray
Byte[] DATA = new utf8encoding (). getbytes (this. textbox1.text );
// Start writing
FS. Write (data, 0, Data. Length );
// Clear the buffer and close the stream
FS. Flush ();
FS. Close ();

}

Method 2: Use streamwriter

// Instantiate a save file dialog box
Savefiledialog Sf = new savefiledialog ();
// Set the file storage type
Sf. Filter = "txt file | *. txt | all files | *.*";
// If the user does not enter the extension, the suffix is automatically appended.
Sf. AddExtension = true;
// Set the title
Sf. Title ="Write files";
// If the user clicks the Save button
If (sf. ShowDialog () = DialogResult. OK)
{
// Instantiate a file stream ---> associated with the written file
FileStream fs = new FileStream (sf. FileName, FileMode. Create );
// Instantiate a StreamWriter --> associated with fs
StreamWriter sw = new StreamWriter (fs );
// Start writing
Sw. Write (this. textBox1.Text );
// Clear the buffer
Sw. Flush ();
// Close the stream
Sw. Close ();
Fs. Close ();
}

Method 3: BinaryWriter

// Instantiate a save file dialog box
SaveFileDialog sf = new SaveFileDialog ();
// Set the file storage type
Sf. Filter = "txt file | *. txt | all files | *.*";
// If the user does not enter the extension, the suffix is automatically appended.
Sf. AddExtension = true;
// Set the title
Sf. Title ="Write files";
// If the user clicks the Save button
If (sf. ShowDialog () = DialogResult. OK)
{
// Instantiate a file stream ---> associated with the written file
FileStream fs = new FileStream (sf. FileName, FileMode. Create );
// Instantiate BinaryWriter
BinaryWriter bw = new BinaryWriter (fs );
Bw. Write (this. textBox1.Text );
// Clear the buffer
Bw. Flush ();
// Close the stream
Bw. Close ();
Fs. Close ();
}
 

C #Cache stream example ------> copy files with cache streams

C #File Processing must first import the namespace: using System.IO;

Background: Use VS2005, a button, a form,C #Cache stream, Copy D: \ KuGoo \ love too many .wma to D: \ and name it love.wma, that is, D: \ love.wma

Add the following code to the Click Event of the button:

P rivate void button#click (object sender, EventArgs e)
{
// Create two file streams. One is related to the source file, and the other is the file to be written.
FileStream fs = new FileStream (@ "D: \ KuGoo \ love too many .wma", FileMode. Open );
FileStream fs2 = new FileStream (@ "D: \ love.wma", FileMode. Create );
// Create a byteArrayAs a medium between the two
// It is like two people taking an apple, this byteArrayIt's like a basket where an apple is sent to the basket as a dead man,
// And I can die to get an apple. Through this media, we don't interfere with each other,
// You do not need to wait for each other until she puts an apple in the basket.
Byte[] Data = newByte[1024];
// Create two buffer streams, which are associated with the two file streams
BufferedStream bs = new BufferedStream (fs );
BufferedStream bs2 = new BufferedStream (fs2 );
// Fs is used as dead read and fs2 is used as dead write until fs has no bytes to read and fs2 will not be written
// For example, if one person throws an apple into the basket, and the other person dies, he can take an apple into the basket until there is no apple in the basket.
// That is, --> there is no apple in the basket.
While (FS. Read (data, 0, Data. Length)> 0)
{
Fs2.write (data, 0, Data. Length );
Fs2.flush ();
}
// Close the stream. It's like two people are tired and have to rest...
FS. Close ();
Fs2.close ();
}

 

C #Memory stream example -----> use memory stream to read images
C #File Processing must first import the namespace: using system.IO;

Background: A form, a picturebox, and a lable [no image selected, lable text is "image not selected"]. Add the following code in the click event of picturebox1:

P rivate void picturebox#click (Object sender, eventargs E)
{
// Instantiate an open file dialog box
Openfiledialog op = new openfiledialog ();
// Set the file type
Op. Filter = "jpg image | *. jpg | GIF image | *. GIF ";
// If you click the OPEN button and select the correct image path, perform the following operations:
If (op. ShowDialog () = DialogResult. OK)
{
// Clear text
This. label1.Text = "";
// Instantiate a file stream
FileStream fs = new FileStream (op. FileName, FileMode. Open );
// Read the object to byteArray
Byte[] Data = newByte[Fs. Length];
Fs. Read (data, 0, data. Length );
Fs. Close ();

// Instantiate a memory stream ---> get the content read from the file stream [bytesArray] Put it in the memory stream
MemoryStream MS = new MemoryStream (data );
// Set the image in pictureBox1.
This. pictureBox1.Image = Image. FromStream (MS );
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.