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

6 篇文章 0 订阅
3 篇文章 0 订阅

感觉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);
	                }
                } 
            }
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值