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; 

转载于:https://www.cnblogs.com/TheJoker/p/7903088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值