WPF和winForm 对 RichTextBox 的读写文件比较

  • Post author:
  • Post category:其他


感觉WinForm和WPF控件属性方法差别很大,我真觉得微软公司都是一群高智商,有足够的时间来干闲事的人,虽然都是一娘生的,但是差别怎么这么大咧!

废话不多说了,我们进入正题

首先来看WinFrom的RichTextBox对文件的读取

FileProcess.SubFolderCheckOrCreate(Application.StartupPath, "LOG");//检查可执行路径下的文件夹是否存在,如果不存在则创建一个LOG文件夹
            RichTextPath = FileProcess.getFullpathByExtension(Application.StartupPath + "\\LOG\\", FileProcess.Entension.RTF);//设置文件读取的路径
            if (RichTextPath != "")
            {
                this.richTextBox1.LoadFile(RichTextPath);//导入文件
            }

WinFrom的RichTextBox对文件的保存

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Regex regex = new Regex(@"[^<>/\\\|:""\*\?\n\r]+");
            Match match = regex.Match(this.richTextBox1.Text.Replace("\\\\",""));//读取RichTextBox的文本,去掉非法路径字符
            if (RichTextPath != "")
            {
                File.Delete(RichTextPath);//如果之前导入的文件存在,则将其删除
            }
            if (match.Success)//若干RichTextBox有文本内容
            {
                FileProcess.SubFolderCheckOrCreate(Application.StartupPath, "LOG");//如果不存在就创建LOG文件夹 
                this.richTextBox1.SaveFile(Application.StartupPath + "\\LOG\\" + match.Value + DateTime.Now.Year + DateTime.Now.Month+DateTime.Now.Day+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second + ".rtf");//保存文件
            }
        }

然后咱们来看一下WPF RichTextBox 控件对文件的读写操作

WPF RichTextBox读取文件

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FileProcess.SubFolderCheckOrCreate(System.Windows.Forms.Application.StartupPath, "LOG");
            RichTextPath = FileProcess.getFullpathByExtension(System.Windows.Forms.Application.StartupPath + "\\LOG\\", FileProcess.Entension.RTF);
            if (RichTextPath != "")
            {
                //this.WorkBench.Selection.Load //.LoadFile(RichTextPath);
                using (FileStream fsRtf = new FileStream(RichTextPath, FileMode.Open, FileAccess.Read))
                {
                    // Place the RTF into RichTextBox.
                    System.Windows.Documents.TextRange tr = new System.Windows.Documents.TextRange(
                       this.WorkBench.Document.ContentStart, this.WorkBench.Document.ContentEnd);
                    tr.Load(fsRtf, DataFormats.Rtf);
                }
            }
            //ResultRichText = new RichTextProcess(richTextBoxWPF1.richTextBox1);
        }

WPF RichTextBox保存文件

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Regex regex = new Regex(@"[^<>/\\\|:""\*\?\n\r]+");
            TextRange textRange = new TextRange(this.WorkBench.Document.ContentStart, this.WorkBench.Document.ContentEnd);
            Match match = regex.Match(textRange.Text.Replace("\\\\", ""));
            if (RichTextPath != "")
            {
                File.Delete(RichTextPath);
            }
            if (match.Success)
            {
                FileProcess.SubFolderCheckOrCreate(System.Windows.Forms.Application.StartupPath, "LOG");//如果不存在就创建file文件夹 
                if (textRange.CanSave(DataFormats.Rtf))
                {
                    using (FileStream fsRtf = new FileStream(System.Windows.Forms.Application.StartupPath + "\\LOG\\" + match.Value + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".rtf", FileMode.Create, FileAccess.Write))
	                {
                        textRange.Save(fsRtf, DataFormats.Rtf);
	                }
                } 
            }
        }



版权声明:本文为qq_16635325原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。