WPF富文本RichTextBox用法

1.加载文本


        /// <summary>
        /// 加载文本
        /// </summary>
        /// <param name="filename"></param>
        private void LoadFile(string filename,RichTextBox   richTextBox)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException();
            }
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException();
            }
            using (FileStream stream = File.OpenRead(filename))
            {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                StreamReader sr = new StreamReader(stream, Encoding.Default);
                documentTextRange.Load(new MemoryStream(Encoding.UTF8.GetBytes(sr.ReadToEnd())), dataFormat);

            }

        }

防止中文乱码:

        StreamReader sr = new StreamReader(stream, Encoding.Default);

        new MemoryStream(Encoding.UTF8.GetBytes(sr.ReadToEnd()))


2.读取文本

         TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

            return Encoding.UTF8.GetBytes(documentTextRange.Text);

待续。。。。。。




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