简体   繁体   English

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

[英]How do I save a rtf file from a WPF RichTextBox in Powershell?

I need to save the file from a RichTextBox WPF that's been created in PowerShell我需要保存在 PowerShell 中创建的RichTextBox WPF 中的文件

I've tried piping through System.Windows.Markup.XamlWriter , and I've been banging my head on tons of stuff I deleted back out of.我已经尝试通过System.Windows.Markup.XamlWriter进行管道传输,并且我一直在努力解决我删除的大量内容。 I just can't seem to get this one.我似乎无法得到这个。

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

The SaveFile Method can be used to do this. SaveFile方法可用于执行此操作。

$RichTextBox.SaveFile('C:\Path\File.rtf')

It will store Font and Colors automatically.它会自动存储Font和Colors。 If plain text output is needed it can be done as follows:如果需要纯文本 output,可以按如下方式完成:

$RichTextBox.SaveFile('C:\Path\File.rtf', 'PlainText')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM