如何在Powershell中从WPF RichTextBox保存rtf文件?

时间:2019-03-23 18:21:50

标签: wpf powershell xaml richtextbox

我需要从在PowerShell中创建的RichTextBox WPF中保存文件

我尝试通过System.Windows.Markup.XamlWriter进行管道传输,我一直在想着删除掉的大量内容。我似乎无法得到这个。

if ($saveFile) {
   $tr = new-object System.Windows.Documents.TextRange($RichEdit.Document.ContentStart,                                                 
                                   $RichEdit.Document.ContentEnd)
   $ascii = (new-Object System.Text.ASCIIEncoding).getbytes($tr)

   #  $docStream = new-Object System.IO.MemoryStream($ascii,$false)
   $docStream = New-Object IO.FileStream $saveFile ,'Append','Write','Read'
   $out = new-object System.Windows.Markup.XamlWriter.Save $tr, $docStream
}

1 个答案:

答案 0 :(得分:0)

if ($saveFile) {
  $RichEdit.SelectAll()
  $as = New-Object IO.FileStream $saveFile ,'Create'
  $a = $RichEdit.Selection.Save($as,[Windows.DataFormats]::Rtf)
}