问题
before adding the text to RTF property in RichTextBox, i make some processing on the text, add escape character then divide the data to multiline.
the text is
line1 \n\u001aline2 \n\u001aline3 \n\u001aline4
When i use VS 2015 with .Net 4.6.2 the value in RTF property is
{\\rtf1\\fbidis\\ansi\\deff0{\\fonttbl{\\f0\\fnil\\fcharset0
Arial;}}\r\n\\viewkind4\\uc1\\pard\\ltrpar\\lang3073\\fs24 Line1
\\par\r\n\\v\\'1a\\v0 Line2
\\par\r\n\\v\\'1a\\v0 Line3
\\par\r\n\\v\\'1a\\v0 Line4\\par\r\n}\r\n
but when i switched to VS 2017 with .Net 4.7.1 the value changed to
{\\rtf1\\fbidis\\ansi\\deff0\\nouicompat{\\fonttbl{\\f0\\fnil\\fcharset0
Arial;}}\r\n{\\*\\generator Riched20 10.0.16299}\\viewkind4\\uc1
\r\n\\pard\\ltrpar\\fs24\\lang1033 Line1
\\par\r\n\\v\\'1a\\v0 Line2 \\par\r\n\\v\\'1a\\v0
Line3 \\par\r\n\r\n\\pard\\ltrpar\\v\\'1a\\v0
Line4\\par\r\n}\r\n
and when i accessed the Text property i got this Text
Line1 \nLine2 \nLine3 \nLine4
RichTextBox removes the escape character in VS 2017 anyone knows Why?
回答1:
When your application targets .NET 4.6.2 (or below), RichTextBox instantiates RichEdit control version3 (versions are described here), when your application is retargeted to .NET 4.7.1, it instantiates Rich Edit version 4.1 (msftedit.dll). Difference in RTF representation is most likely caused by the newer version of the control. You can opt-out from using the newer version of Rich Edit even when your application targets 4.7 and above by adding app.config file with the following compatibility switch under AppContextSwitchOverrides tag:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/>
</startup>
<runtime>
<AppContextSwitchOverrides
value="Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl=true" />
</runtime>
</configuration>
Conversely you can load Rich Edit 4.1 in an application that targets .NET 4.6.2 by setting the above AppContextSwitch to false.
Hope this helps, Tanya
来源:https://stackoverflow.com/questions/48371211/richtextbox-removes-escape-character-from-the-text