C# 读取富格文本格式时出现富格文本框问题
我的应用程序有问题。我这样做是为了用我的应用程序双击打开一个文件:C# 读取富格文本格式时出现富格文本框问题,c#,visual-studio,winforms,exception,registry,C#,Visual Studio,Winforms,Exception,Registry,我的应用程序有问题。我这样做是为了用我的应用程序双击打开一个文件: public void ProcessText() { Path = Args[Args.Length - 1]; System.Windows.Forms.RichTextBox TB = new System.Windows.Forms.RichTextBox(); TB.Text = Path; ContentTextBox.Text = System
public void ProcessText()
{
Path = Args[Args.Length - 1];
System.Windows.Forms.RichTextBox TB = new System.Windows.Forms.RichTextBox();
TB.Text = Path;
ContentTextBox.Text = System.IO.File.ReadAllText(TB.Text);
if (Args.Length == 1)
ContentTextBox.Text = ET;
}
我正在尝试使用注册表在我的应用程序中打开一个文件,它可以工作,但当我尝试读取富文本格式时,它会显示以下内容:
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}
{\colortbl;\red255\green255\blue255;}
{*\generator Riched20 10.0.19041}\viewkind4\uc1
\pard\cf1\f0\fs18 GTFYRDETFYGUKKILJOULHKGTJFRHDEGSW\f1\par
}
不管这意味着什么
我试着让它看起来像这样:
“tgyhjumki,Ujhytgrgfghbnjmk”
我使用了“打开文件”对话框中的代码:
ContentTextBox.LoadFile(OFD.FileName);
结果是这样的:
public void ProcessText()
{
Path = Args[Args.Length - 1];
System.Windows.Forms.RichTextBox TB = new System.Windows.Forms.RichTextBox();
TB.Text = Path;
ContentTextBox.LoadFile(TB.Text);
if (Args.Length == 1)
ContentTextBox.Text = ET;
}
使用此代码后,它告诉我有一个错误:System.ArgumentException:“文件格式无效。”
请帮助我在不使用注册表编辑器的情况下解决此问题。我做了一些研究,发现了一些问题:首先,是的,我在回答我自己的问题,但我这样做是为了让您理解。我使用的代码如下:
public void ProcessText()
{
Path = Args[Args.Length - 1];
System.Windows.Forms.RichTextBox TB = new System.Windows.Forms.RichTextBox { Text = Path };
try
{
ContentTextBox.LoadFile(Path);
}
catch
{
ContentTextBox.Text = System.IO.File.ReadAllText(TB.Text);
}
if (Args.Length == 1)
ContentTextBox.Text = ET;
}
这意味着:
当我尝试对富文本文档使用simple text方法时,出现了一个错误。抛出异常是因为文本编辑器中没有任何格式的打开文件,所以我让它尝试打开的文本,当它失败时,它尝试其他内容,如果不起作用,文本将为空。这就是解决办法。也谢谢你的建议-他们帮我解决了这个问题-你们真是太棒了测试框中的编码是WindowsEncoding1252。因此,要正确显示,您需要一种支持1252编码的字体。它不允许我更改字体。我需要一个C代码中的解决方案你是如何创建文件的?你需要设置RTF属性,而不是文本!!你看到的是原始RTF内容,即卷曲中的各种命令..System.ArgumentException:“路径中的非法字符”-这就是我使用RTF而不是TextShort时显示的内容:现在,当我打开一个简单的文本文件时,它将显示一个简单的文本文件,当我打开一个富文本文件时,它将显示一个富文本文件