/// <summary> /// 处理打开文档或插入文档 /// </summary> /// <param name="IsInsertFile"></param> public void LoadOrInsertFile(bool IsInsertFile, string FileName) { try { if (File.Exists(FileName) == false) { return; } string FileExt = System.IO.Path.GetExtension(FileName).ToUpper(); //文件扩展名 TextRange range = null; if (IsInsertFile) { range = _rtf.Selection; } else { range = new TextRange(_rtf.Document.ContentStart, _rtf.Document.ContentEnd); //用户输入了些东西 if (MySuperEditorHelper.IsPrintableString(range.Text)) { MessageBoxResult ret = MessageBox.Show("保存旧文档吗?", "保存文档", MessageBoxButton.YesNo); if (ret == MessageBoxResult.Yes) { SaveToFile(true); } } CurFileName = FileName; } //打开文件 using (FileStream fs = new FileStream(FileName, FileMode.Open)) { switch (FileExt) { case ".XAMLPACKAGE": range.Load(fs, DataFormats.XamlPackage); break; case ".XAML": range.Load(fs, DataFormats.Xaml); break; case ".RTF": range.Load(fs, DataFormats.Rtf); break; case ".TXT": default: //由于TextRange对象不能直接装入Unicode字符,所以,采用其他方法进行处理 range.Text = MySuperEditorHelper.LoadStringFromTextFile(fs); break; } } } catch (Exception ex) { throw ex; } }
/// <summary> /// 新建文档 /// </summary> public void NewDocument() { try { TextRange range = new TextRange(_rtf.Document.ContentStart, _rtf.Document.ContentEnd); if (MySuperEditorHelper.IsPrintableString(range.Text)) { MessageBoxResult ret = MessageBox.Show("保存旧文档吗?", "保存文档", MessageBoxButton.YesNo); if (ret == MessageBoxResult.Yes) { SaveToFile(true); } } range.Text = ""; CurFileName = "无标题"; } catch (Exception ex) { throw ex; } }