標籤:
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;using System.IO;//匯入命名空間--流namespace WindowsFormsApplication2{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 剪下TToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut(); } private void 複製CToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Copy(); } private void 粘貼PToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Paste(); } private void 全選AToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.SelectAll(); } private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo(); } private string FileName; private void 開啟OToolStripMenuItem_Click(object sender, EventArgs e) { //建立開啟檔案對話方塊:幫我們去硬碟上尋找檔案路徑 // OpenFileDialog openfiledialog1 = new OpenFileDialog();//建立對象 openFileDialog1.Filter = "文字檔|*.txt|所有檔案|*.*";//過濾 DialogResult isok = openFileDialog1.ShowDialog();//顯示對話方塊 if (isok == DialogResult.OK)//通過對話方塊的傳回值來判斷是否點的開啟按鈕 { //擷取檔案路徑 string filename = openFileDialog1.FileName; FileName = filename; //根據檔案路徑去讀取檔案:使用流來讀取 //FileStream fs = new FileStream(filename,FileMode.Open); //StreamReader sr = new StreamReader(fs,Encoding.Default);//流讀取器--Encoding.Default 選擇預設編碼 StreamReader sr = new StreamReader(filename,Encoding.Default); string text = sr.ReadToEnd();//讀取 sr.Close(); // fs.Close(); textBox1.Text = text; } } private void 儲存SToolStripMenuItem_Click(object sender, EventArgs e) { if (FileName != "") { FileStream fs = new FileStream(FileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default);//流寫入 sw.Write(textBox1.Text); sw.Close(); fs.Close(); } else { SaveFileDialog savefiledialog1 = new SaveFileDialog(); savefiledialog1.Filter = "文字檔|*.txt|所有檔案|*.*"; DialogResult isok = savefiledialog1.ShowDialog(); if (isok == DialogResult.OK) { string filename = savefiledialog1.FileName; FileStream fs = new FileStream(filename, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default);//流寫入 sw.Write(textBox1.Text); sw.Close(); fs.Close(); } } } private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void 尋找ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f = new Form2(textBox1.SelectedText,this); f.Owner = this; f.Show(); } private void 替換ToolStripMenuItem_Click(object sender, EventArgs e) { } private void 預覽列印VToolStripMenuItem_Click(object sender, EventArgs e) { printPreviewDialog1.ShowDialog(); } private void 列印PToolStripMenuItem_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1;//指定列印對話方塊所對應的列印對象 DialogResult isok = printDialog1.ShowDialog(); if (isok == DialogResult.OK) { printDocument1.Print();//列印文檔 } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //設定列印內容 Font f = new System.Drawing.Font("宋體",6); e.Graphics.DrawString(textBox1.Text,f,System.Drawing.Brushes.Black,new PointF(0,0)); } }}
使用流讀取器注意命名空間的引用
簡單的記事本功能實現代碼--(流讀取器)