Tech Off Post

Single Post Permalink

View Thread: wpf richtextbox
  • User profile image
    footballism

    public void LoadFile(String path, RichTextBox richTextBox)
    {
        if (!File.Exists(path)) throw new FileNotFoundException("the specified file doesn't exist.");
        String fileExtension = System.IO.Path.GetExtension(path).ToUpper();
        MessageBox.Show(fileExtension.ToString());
        String dataFormat = DataFormats.Text;
        if (fileExtension == "TXT")
        {
            dataFormat = DataFormats.Text;
        }
        else if (fileExtension == "XAML")
        {
            dataFormat = DataFormats.Xaml;
        }
        else if (fileExtension == "RTF")
        {
            dataFormat = DataFormats.Rtf;
        }
        using (FileStream fileStream = File.OpenRead(path))
        {
            TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            if (textRange.CanLoad(dataFormat))
            {
                textRange.Load(fileStream, dataFormat);
            }
        }
    }

    Currently WPF's RichTextBox only supports reading txt, xaml, rtf or xaml package.

    Sheva