c# 记事本实现代码
2009-06-11 08:31:56 来源:WEB开发网switch (d)
{
case DialogResult.Yes:
using (SaveFileDialog saveDig = new SaveFileDialog())
{
saveDig.Filter = @"文本文档(*.txt)|*.txt";
saveDig.FileName = "*.txt";
if (saveDig.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveDig.FileName, false, System.Text.Encoding.Default);
sw.Write(this.textBox1.Text);
sw.Close();
this.Text = saveDig.FileName;
}
}
break;
case DialogResult.No:
this.textBox1.Text = "";
break;
case DialogResult.Cancel:
break;
default: System.Diagnostics.Debug.Assert(false);
break;
}
}
else
{
using (OpenFileDialog dlgText = new OpenFileDialog())
{
dlgText.Filter = @"(*.txt)|*.txt";
if (dlgText.ShowDialog() == DialogResult.OK)
{
if (File.Exists(dlgText.FileName))
{
StreamReader rStream = new StreamReader(dlgText.FileName, System.Text.Encoding.Default);
string s=default(string);
this.textBox1.Text = "";
while ((s = rStream.ReadLine()) != null)
{
this.textBox1.Text += s;
}
rStream.Close();
}
}
}
}
}
private void MyNotePad_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.textBox1.Text != "")
{
DialogResult d = MessageBox.Show("文件 " + this.Text + @" 的文字已经改变。
想保存文件吗?", "MyNotePad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
switch (d)
{
case DialogResult.Yes:
using (SaveFileDialog saveDig = new SaveFileDialog())
{
saveDig.Filter = @"文本文档(*.txt)|*.txt";
saveDig.FileName = "*.txt";
if (saveDig.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveDig.FileName, false, System.Text.Encoding.Default);
sw.Write(this.textBox1.Text);
sw.Close();
this.Text = saveDig.FileName;
}
}
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
break;
default: System.Diagnostics.Debug.Assert(false);
break;
}
}
}
}
}
其他代码可以省略吧。
更多精彩
赞助商链接