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

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

範列1.讀取圖檔後存檔

程式碼 

 

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)
        {
            //呈現對話盒
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //檔案名稱
                //MessageBox.Show(this.openFileDialog1.FileName);
                //1.建立串流
                System.IO.Stream fs1 = new System.IO.FileStream(this.openFileDialog1.FileName, 
                    System.IO.FileMode.Open, System.IO.FileAccess.Read);
                //準備緩衝區 Byte陣列 
                Byte[] buff = new Byte[fs1.Length];
                //開始讀取 讀到緩衝區去
                fs1.Read(buff, 0, (Int32)fs1.Length);
                //建立Gap(記憶體串流)
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                ms.Write(buff, 0, (Int32)fs1.Length);
                //建構Image物件
                System.Drawing.Image img = new System.Drawing.Bitmap(ms);
                //設定PictureBox
                this.pictureBox1.Image = img;
                //寫出圖片到另一個地方 
                //建立串流 
                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);
                //寫出去
                fout.Write(buff, 0, (Int32)fs1.Length);
                //清緩衝區
                fout.Flush();
                fout.Close();
                //關閉串流
                fs1.Close();
            }
        }
    }
}

範列2.筆記本 讀檔與寫檔

程式碼

 

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)
        {
            //啟動對話盒
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //處理
                //1.建立串流 
                System.IO.Stream fs1 = new System.IO.FileStream(this.openFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                //2.接上讀取器
                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)
            {
                //儲存
                //1.建立串流
                System.IO.Stream fs1 = new System.IO.FileStream(this.saveFileDialog1.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                //2.建立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();               

            }
        }
    }
}

補充: