阅读富文本格式时的富文本框问题
Rich Text Box problem when reading a Rich Text Format
我的应用程序有问题。我做这个是为了用我的应用程序双击打开一个文件:
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 gtfyrdetfyguhkiljoulhykgtjfrhdegsw\f1\par
}
不管这意味着什么...
我试着让它看起来像这样:
"tgyhjumki,ujhytgrfghbnjmk"
我使用了“打开文件”对话框中的代码:
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: 'File format is not valid.'
请帮助我在不使用注册表编辑器的情况下解决此问题
我做了一些研究,发现了一些东西:首先,是的,我正在回答我自己的问题,但我这样做是为了让你理解。我使用的代码是这样的:
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;
}
这意味着:
当我尝试对富文本文档使用简单文本方法时,出现错误。抛出异常是因为在文本编辑器中没有打开任何格式的文件,所以我让它尝试打开文本,当它失败时,它会尝试其他东西,如果 none 工作,文本将为空。这就是解决方案。也感谢您的建议 - 他们帮助我解决了这个问题 - 你们真棒
我的应用程序有问题。我做这个是为了用我的应用程序双击打开一个文件:
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 gtfyrdetfyguhkiljoulhykgtjfrhdegsw\f1\par }
不管这意味着什么...
我试着让它看起来像这样:
"tgyhjumki,ujhytgrfghbnjmk"
我使用了“打开文件”对话框中的代码:
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: 'File format is not valid.'
请帮助我在不使用注册表编辑器的情况下解决此问题
我做了一些研究,发现了一些东西:首先,是的,我正在回答我自己的问题,但我这样做是为了让你理解。我使用的代码是这样的:
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;
}
这意味着:
当我尝试对富文本文档使用简单文本方法时,出现错误。抛出异常是因为在文本编辑器中没有打开任何格式的文件,所以我让它尝试打开文本,当它失败时,它会尝试其他东西,如果 none 工作,文本将为空。这就是解决方案。也感谢您的建议 - 他们帮助我解决了这个问题 - 你们真棒