分享给大家,有不好的地方请指教
----------------------------------------记事本-----------------------------------------
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 MyNotepad
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string path = dialog.FileName;
StreamWriter sw = new StreamWriter(path); //创建文件写入对象
string txt=textBox1.Text;
sw.Write(txt); //写入文件
sw.Close(); //关闭对象
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.FileName;
StreamReader sr = new StreamReader(path,Encoding.Default);//创建文件读取对象
textBox1.Text = sr.ReadToEnd(); //读取文件内容
sr.Close(); //关闭文件
}
}
}
}
|
|