zoukankan      html  css  js  c++  java
  • 【C#】利用富文本框打开,保存text文件【源代码】

    private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)

            {

                OpenFileDialog open = new OpenFileDialog();

                open.InitialDirectory = "F:\\";     //设置初始目录

                open.Filter = "文本文件(*.txt)|*.*|(*.htm)|*.htm"; //打开htm类型文件或text文件

                open.RestoreDirectory = true;   //设置对话框在关闭前还原当前目录

                open.FilterIndex = 1;

                if (open.ShowDialog() == DialogResult.OK)

                {

                    string str = open.FileName;

                    StreamReader sr = File.OpenText(str);

                    string s;

                    while ((s = sr.ReadLine()) != null)

                        this.richTextBox1.Text += s;

                    sr.Close();

                }

            }

     

            private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)

            {

                SaveFileDialog save = new SaveFileDialog();

                save.InitialDirectory = "C:\\";

                save.Filter = "(*.txt)|*.*|(*.cs)|*.cs";    //打开cs文件或text文件

                save.RestoreDirectory = true;

                save.FilterIndex = 1;

                if (save.ShowDialog() == DialogResult.OK)

                {

                    string str = save.FileName;

                    //向指定的文件中追加内容,如果文件不存在,则创建文件

                    StreamWriter sw = File.AppendText(str);

                    sw.Write(this.richTextBox1.Text);

                    sw.Flush();

                    sw.Close();

                }

            }

     

  • 相关阅读:
    [LeetCode] 852. Peak Index in a Mountain Array
    [LeetCode] 221. Maximal Square
    [LeetCode] 260. Single Number III
    [LeetCode] 532. K-diff Pairs in an Array
    [LeetCode] 1417. Reformat The String
    [LeetCode] 621. Task Scheduler
    [LeetCode] 454. 4Sum II
    [LeetCode] 18. 4Sum
    [LeetCode] 369. Plus One Linked List
    [LeetCode] 380. Insert Delete GetRandom O(1)
  • 原文地址:https://www.cnblogs.com/ngnetboy/p/2390136.html
Copyright © 2011-2022 走看看