简体   繁体   English

如何使用Powershell在WPF中显示rtf文件?

[英]How to display an rtf file in WPF with Powershell?

I've been googling in vain for hours, and can't seem to find a way to view an rtf file in a powershell WPF form. 我已经无聊地搜寻了几个小时,而且似乎找不到找到以Powershell WPF格式查看rtf文件的方法。

I can get the rtf file using: 我可以使用以下方法获取rtf文件:

$myContent = gc c:\myContent.rtf

but when I try to display it using: 但是当我尝试使用以下方法显示它时:

$RichTextBox.appendText($myContent)

I get the encoded rtf, not the properly formatted content. 我得到了编码的rtf,而不是格式正确的内容。

Anyone have a way to do this? 有人有办法吗? Loads of examples out there about how to do this in c#, but none for PowerShell. 关于如何在c#中执行此操作的示例很多,但对于PowerShell则没有。

Thanks, 谢谢,

Ben

.AppendText only works with strings, not raw RTF. .AppendText仅适用于字符串,不适用于原始RTF。 RTF is a sequence of control codes mixed with raw text. RTF是混合有原始文本的一系列控制代码。 You need to use a different method in order to parse it: 您需要使用其他方法来解析它:

$stream = new-object IO.MemoryStream (`
       [Text.ASCIIEncoding]::Default.GetBytes($myContent))
$RichTextBox.Selection.Load($stream, [Windows.DataFormats]::Rtf)

Hope this helps, 希望这可以帮助,

-Oisin -Oisin

OK - so I finally got this to work with some tweaking of Oisin's post. 好的-所以我终于对Oisin的帖子进行了一些调整。

Will mark his as the "proper" answer, as I wouldn't have got here without him, but thought I'd post my code in case it helps anyone in future: 会将他标记为“正确”答案,因为没有他我就不会在这里,但我想我会发布代码,以防将来对任何人有帮助:

$myContent = gc "c:\myContent.rtf"
$ascii = (new-Object System.Text.ASCIIEncoding).getbytes($myContent)
$stream = new-Object System.IO.MemoryStream($ascii,$false)
$RichTextBox.Selection.Load($stream, [Windows.DataFormats]::Rtf) 

Cheers, 干杯,

Ben

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

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