RichTextBox rtf = new RichTextBox();
using (StreamReader sr = new StreamReader(inpath))
{
rtf.Rtf = sr.ReadToEnd();
}

解决方案 »

  1.   

    谢谢楼上,不过看不明白,如果现在有了一个Stream s,  怎么把这个s保存到路径X的文件里去?
      

  2.   


    byte[] bytes = new byte[s.Length];
    s.Write(bytes,0,bytes.Length);
    File.WriteAllBytes("路径",bytes);
      

  3.   

    必须有人勇敢地站出来指出#5楼的错误s.Write(bytes,0,bytes.Length);FileStream.Write  使用从缓冲区读取的数据将字节块写入该流。
    改为s.Read(byte, 0, s.Length);FileStream.Read   从流中读取字节块并将该数据写入给定缓冲区中。
      

  4.   

    笔误s.Read(bytes, 0, s.Length);
      

  5.   

    你的源代码是不是有点小问题,
    s.Read(byte, 0, s.Length);
    这个byte应该是data吧,只要把data里面的数据存入文档就行了FileStream fs = new FileStream("C:\temp.txt",FileMode.OpenorCreate);
    BinaryWrite bw = new BinaryWrite(fs);
    bw.write(data);
    bw.flush();
      

  6.   

    谢谢各位!
    通过下面这个代码,我把一个richtextbox(叫做rtt)里面的内容放到一个stream里,然后又把它存到了一个.rtf文件。
    FlowDocument document = rtt.Document;
                System.IO.Stream ms = new System.IO.MemoryStream();
                System.Windows.Markup.XamlWriter.Save(document, ms);
                byte[] data = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(data, 0, data.Length);
                ms.Close();
                File.WriteAllBytes(路径, data);
    但问题是这个rtt里的内容,当存到文件里时,存为了下面这种格式,
    <FlowDocument PagePadding="5,0,5,0" AllowDrop="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph>RichTextBoxggg<Run xml:lang="en" xml:space="preserve"> it is just a test!!!!</Run></Paragraph><BlockUIContainer><RichTextBox IsReadOnly="True" Name="rtb2"><FlowDocument PagePadding="5,0,5,0" AllowDrop="True"><Paragraph>sdlf</Paragraph></FlowDocument></RichTextBox></BlockUIContainer></FlowDocument>
    我怎么才能让它在文件里,保存为在wpf里显示的这种格式?