【问题标题】:Rich Text Box problem when reading a Rich Text Format读取富文本格式时的富文本框问题
【发布时间】:2021-04-19 12:15:37
【问题描述】:

我的应用有问题。我这样做是为了用我的应用程序双击打开文件:

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.'

请帮我在不使用注册表编辑器的情况下解决此问题

【问题讨论】:

  • 测试框中的编码是Windows Encoding 1252,所以要正常显示需要支持1252编码的字体。
  • 它不允许我更改字体。我需要 C# 代码中的解决方案
  • 你是如何创建文件的?
  • 您需要设置 RTF 属性,而不是文本!你看到的是原始的 RTF 内容,即各种花括号中的命令..
  • System.ArgumentException: '路径中有非法字符。' - 这就是当我使用 Rtf 而不是 Text 时它向我展示的内容

标签: c# visual-studio winforms exception registry


【解决方案1】:

我做了一些研究,发现了一些东西:首先,是的,我正在回答我自己的问题,但我这样做是为了让您理解。我使用的代码是这样的:

    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;
    }

这意味着:

当我尝试对富文本文档使用简单文本方法时,出现错误。抛出异常是因为文本编辑器中没有打开任何格式的文件,所以我让它尝试打开的文本,当它失败时,它会尝试其他东西,如果没有工作,文本将为空。这就是解决方案。还要感谢您的建议-他们帮助我解决了这个问题-你们太棒了?????

【讨论】:

  • 简短:现在当我打开一个简单的文本文件时,它会显示一个简单的文本文件
  • 当我打开一个富文本文件时,它会显示一个富文本文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多