winform 保存文件 打开文件 选择文件 字体样式颜色(流 using System.IO;)
1 string filePath = ""; 2 3 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) 4 { 5 if (filePath.Length <= 0) 6 { 7 saveFileDialog1.Filter = "文本文件|*.txt"; 8 saveFileDialog1.FileName = "新建文本文件"; 9 DialogResult dr = saveFileDialog1.ShowDialog(); 10 11 if (dr == DialogResult.OK) 12 { 13 string path = saveFileDialog1.FileName; 14 StreamWriter sw = new StreamWriter(path); 15 sw.Write(textBox1.Text); 16 sw.Flush(); 17 filePath = path; 18 } 19 } 20 else 21 { 22 StreamWriter sw = new StreamWriter(filePath); 23 sw.Write(textBox1.Text); 24 sw.Flush(); 25 } 26 27 }
保存文件
1 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 openFileDialog1.Filter = "文本文件|*.txt|C#代码文件|*.cs|全部文件|*.*"; 4 DialogResult dr = openFileDialog1.ShowDialog(); 5 if (dr == DialogResult.OK) 6 { 7 string path = openFileDialog1.FileName; 8 9 10 StreamReader sr = new StreamReader(path, Encoding.Default); 11 textBox1.Text = sr.ReadToEnd(); 12 13 string[] name = openFileDialog1.FileName.Split('\\'); 14 15 this.Text = name[name.Length - 1] + " - 记事本"; 16 17 } 18 }
打开文件
1 private void 选择文件夹ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 DialogResult dr = folderBrowserDialog1.ShowDialog(); 4 if (dr == DialogResult.OK) 5 { 6 textBox1.Text = folderBrowserDialog1.SelectedPath; 7 8 } 9 }
选择文件
1 private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 DialogResult dr = fontDialog1.ShowDialog(); 4 5 if (dr == DialogResult.OK) 6 { 7 textBox1.Font = fontDialog1.Font; 8 textBox1.ForeColor = fontDialog1.Color; 9 10 11 } 12 }
字体样式 颜色
1 using System.IO;