(2010-08-11) C#.NET System.IO

Summary: (2010-08-06) C#.NET System.IO

Fan list 1. File after reading the drawing

Program< /p>

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winmod05
{
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// present dialog box
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
//File name
//MessageBox.Show(this.openFileDialog1.FileName);
//1. Create a stream
System.IO.Stream fs1 = new System.IO.FileStream(this.openFileDialog1.FileName,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
//Prepare the buffer Byte array
Byte[] buff = new Byte[fs1.Length];
//Start reading and read to the buffer
fs1.Read(buff, 0, (Int32)fs1.Length);
//Establish Gap (memory streaming)
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(buff, 0, (Int32)fs1.Length);
//Construct Image object
System.Drawing.Image img = new System.Drawing.Bitmap(ms);
//Set PictureBox
this.pictureBox1.Image = img;
//Write the picture to another place
//Create a stream
String newFile=System.IO.Path.GetFileNameWithoutExtension(this.openFileDialog1.FileName)+"_new.jpg";
//MessageBox.Show(newFile);
System.IO.Stream fout =
new System.IO.FileStream(@"c:images" + newFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
//Write it out
fout.Write(buff, 0, (Int32)fs1.Length);
//Clear buffer
fout.Flush();
fout.Close();
//Close the stream
fs1.Close();
}
}
}
}

Fan list 2. Notebook reading and writing files

Program

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winmod05
{
public partial class NotePad: Form
{
public NotePad()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//Start the dialog box
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
//Handle
//1. Create a stream
System.IO.Stream fs1 = new System.IO.FileStream(this.openFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
//2. Connect the reader
System.IO.TextReader reader = new System.IO.StreamReader(fs1, System.Text.Encoding.UTF8);
String text = reader.ReadToEnd();
this.textBox1.Text = text;
fs1.Close();
}
}

private void button2_Click(object sender, EventArgs e)
{
//
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//Save
//1. Create a stream
System.IO.Stream fs1 = new System.IO.FileStream(this.saveFileDialog1.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
//2. Create Writer
System.IO.StreamWriter writer = new System.IO.StreamWriter(fs1, System.Text.Encoding.UTF8);
writer.Write(this.textBox1.Text);
writer.Flush();
writer.Close();
fs1.Close();

}
}
}
}

Supplement:

Original text: Large column (2010-08-11) C#.NET System.IO

< /p>

Leave a Comment

Your email address will not be published.