C# 使用RichTextBox控件显示文本文件的所有字符

C# 使用RichTextBox控件显示文本文件的所有字符,c#,.net,special-characters,richtextbox,C#,.net,Special Characters,Richtextbox,我想读取文本文件并在RichTextBox控件中显示,并显示所有可能的字符,包括ESC FF SO SI和空格 我不知道怎样才能这样做 此代码没有帮助 if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string text = File.ReadAllText(openFileDialog1.FileName); richTextBox1.Text = text; } 有什

我想读取文本文件并在RichTextBox控件中显示,并显示所有可能的字符,包括ESC FF SO SI空格

我不知道怎样才能这样做

此代码没有帮助

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
    string text = File.ReadAllText(openFileDialog1.FileName);
    richTextBox1.Text = text;
}

有什么线索吗?

一种方法是通过迭代加载文件中的所有字符来生成richtext,但是如果您发现一个特殊的序列字符,请将其替换为序列的粗体/非粗体/斜体名称。看一看如何使用这些格式选项创建RTF文档,然后您可以使用
richTextBox

编辑:好吧,一个RTF编写器对于这个任务来说可能有点过头了。RTFBox的可能性非常有限,但这一个没有问题。看看这个文件。

它包含从0到127的所有ASCII字符。下面是我的应用程序打印它们的方式:

代码很简单。我使用字典来替换特殊字符,并使用扩展名from来获得一些颜色。如果你想要更华丽的色彩和格式(比如单词周围平滑的圆形边缘),不要使用
RichTextBox
,而是更高级的东西。这应该让你开始

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace FileViewer
{
    public partial class FileViewerForm : Form
    {
        public FileViewerForm()
        {
            InitializeComponent();
        }

        //Click handler.
        private void btnLoad_Click(object sender, EventArgs e)
        {
            //Ask them to select a file.
            openFileDialog.Title = "Please select a file";
            openFileDialog.Filter = "Any file|*.*";
            var dlgResult = openFileDialog.ShowDialog();
            if (dlgResult != DialogResult.OK)
                return;
            richTextBox1.Clear();

            //Get the text as a char array.
            char[] text = File.ReadAllText(openFileDialog.FileName).ToCharArray();

            //loop through all of them
            for (int i = 0; i < text.Length; i++)
            {
                //is this a special ASCII character?
                if (lSpecialDict.ContainsKey(text[i]))
                {
                    string replacement;
                    //get the replacement
                    lSpecialDict.TryGetValue(text[i], out replacement);
                    if (replacement != null)
                        //Print it out with DarkGray as backcolor, Firebrick as font color.
                        richTextBox1.AppendText("[" + replacement + "]", Color.LightGray, Color.Firebrick);
                }
                //just a normal character? Then append it.
                else
                    richTextBox1.AppendText(text[i].ToString());
            }
        }

        //Contains the substition strings for the characters. A char --> string mapping.
        private Dictionary<char, string> lSpecialDict 
            = new Dictionary<char, string>()
        {
            { '\0',      "NUL" }, {(char)0x01, "SOH" }, {(char)0x02, "STX" },
            {(char)0x03, "ETX" }, {(char)0x04, "EOT" }, {(char)0x05, "ENQ" },
            {(char)0x06, "ACK" }, {(char)0x07, "BEL" }, {(char)0x08, "BS"  },
            {(char)0x09, "HT"  }, {(char)0x0A, "LF"  }, {(char)0x0B, "VT"  },
            {(char)0x0C, "FF"  }, {(char)0x0D, "CR"  }, {(char)0x0E, "SO"  },
            {(char)0x0F, "SI"  }, {(char)0x10, "DLE" }, {(char)0x11, "DC1" },
            {(char)0x12, "DC2" }, {(char)0x13, "DC3" }, {(char)0x14, "DC4" },
            {(char)0x15, "NAK" }, {(char)0x16, "SYN" }, {(char)0x17, "ETB" },
            {(char)0x18, "CAN" }, {(char)0x19, "EM"  }, {(char)0x1A, "SUB" },
            {(char)0x1B, "ESC" }, {(char)0x1C, "FS"  }, {(char)0x1D, "GS"  },
            {(char)0x1E, "RS"  }, {(char)0x1F, "US"  }, {(char)0x7F, "DEL" },
        };

        private void FileViewerForm_Load(object sender, EventArgs e)
        {

        }
    }
    public static class RichTextBoxExtensions
    {
        public static void AppendText(this RichTextBox box, string text, System.Drawing.Color bgcolor, Color fontcolor)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            var saved = box.SelectionBackColor;
            var saved2 = box.SelectionColor;
            box.SelectionBackColor = bgcolor;
            box.SelectionColor = fontcolor;
            box.AppendText(text);
            box.SelectionBackColor = saved;
            box.SelectionColor = saved2;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统图;
使用System.IO;
使用System.Windows.Forms;
命名空间文件查看器
{
公共部分类FileViewPerform:表单
{
公共文件ViewPerform()
{
初始化组件();
}
//单击处理程序。
私有void btnLoad_单击(对象发送方,事件参数e)
{
//让他们选择一个文件。
openFileDialog.Title=“请选择一个文件”;
openFileDialog.Filter=“任何文件|*.*”;
var dlgResult=openFileDialog.ShowDialog();
if(dlgResult!=DialogResult.OK)
返回;
richTextBox1.Clear();
//以字符数组的形式获取文本。
char[]text=File.ReadAllText(openFileDialog.FileName).ToCharArray();
//循环浏览所有这些内容
for(int i=0;i字符串映射。
专用词典
=新字典()
{
{'\0',“NUL”},{(char)0x01,“SOH”},{(char)0x02,“STX”},
{(char)0x03,“ETX”},{(char)0x04,“EOT”},{(char)0x05,“ENQ”},
{(字符)0x06,“ACK”},{(字符)0x07,“BEL”},{(字符)0x08,“BS”},
{(字符)0x09,“HT”},{(字符)0x0A,“LF”},{(字符)0x0B,“VT”},
{(char)0x0C,“FF”},{(char)0x0D,“CR”},{(char)0x0E,“SO”},
{(char)0x0F,“SI”},{(char)0x10,“DLE”},{(char)0x11,“DC1”},
{(字符)0x12,“DC2”},{(字符)0x13,“DC3”},{(字符)0x14,“DC4”},
{(char)0x15,“NAK”},{(char)0x16,“SYN”},{(char)0x17,“ETB”},
{(字符)0x18,“CAN”},{(字符)0x19,“EM”},{(字符)0x1A,“SUB”},
{(char)0x1B,“ESC”},{(char)0x1C,“FS”},{(char)0x1D,“GS”},
{(char)0x1E,“RS”},{(char)0x1F,“US”},{(char)0x7F,“DEL”},
};
私有无效文件视图执行加载(对象发送方,事件参数e)
{
}
}
公共静态类RichTextBoxExtensions
{
公共静态无效文本(此RichTextBox框,字符串文本,System.Drawing.Color bgcolor,Color fontcolor)
{
box.SelectionStart=box.TextLength;
box.SelectionLength=0;
var saved=box.SelectionBackColor;
var saved2=box.SelectionColor;
box.SelectionBackColor=bgcolor;
box.SelectionColor=fontcolor;
框。追加文本(文本);
box.SelectionBackColor=已保存;
box.SelectionColor=saved2;
}
}
}

一种方法是通过迭代加载文件中的所有字符,自己生成richtext,但如果找到特殊的序列字符,请将其替换为序列的粗体/非粗体/斜体名称。看一看如何使用这些格式选项创建RTF文档,然后您可以使用
richTextBox

编辑:好吧,一个RTF编写器对于这个任务来说可能有点过头了。RTFBox的可能性非常有限,但这一个没有问题。看看这个文件。

它包含从0到127的所有ASCII字符。下面是我的应用程序打印它们的方式:

代码很简单。我使用字典来替换特殊字符,并使用扩展名from来获得一些颜色。如果你想要更华丽的色彩和格式(比如单词周围平滑的圆形边缘),不要使用
RichTextBox
,而是更高级的东西。这应该让你开始

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace FileViewer
{
    public partial class FileViewerForm : Form
    {
        public FileViewerForm()
        {
            InitializeComponent();
        }

        //Click handler.
        private void btnLoad_Click(object sender, EventArgs e)
        {
            //Ask them to select a file.
            openFileDialog.Title = "Please select a file";
            openFileDialog.Filter = "Any file|*.*";
            var dlgResult = openFileDialog.ShowDialog();
            if (dlgResult != DialogResult.OK)
                return;
            richTextBox1.Clear();

            //Get the text as a char array.
            char[] text = File.ReadAllText(openFileDialog.FileName).ToCharArray();

            //loop through all of them
            for (int i = 0; i < text.Length; i++)
            {
                //is this a special ASCII character?
                if (lSpecialDict.ContainsKey(text[i]))
                {
                    string replacement;
                    //get the replacement
                    lSpecialDict.TryGetValue(text[i], out replacement);
                    if (replacement != null)
                        //Print it out with DarkGray as backcolor, Firebrick as font color.
                        richTextBox1.AppendText("[" + replacement + "]", Color.LightGray, Color.Firebrick);
                }
                //just a normal character? Then append it.
                else
                    richTextBox1.AppendText(text[i].ToString());
            }
        }

        //Contains the substition strings for the characters. A char --> string mapping.
        private Dictionary<char, string> lSpecialDict 
            = new Dictionary<char, string>()
        {
            { '\0',      "NUL" }, {(char)0x01, "SOH" }, {(char)0x02, "STX" },
            {(char)0x03, "ETX" }, {(char)0x04, "EOT" }, {(char)0x05, "ENQ" },
            {(char)0x06, "ACK" }, {(char)0x07, "BEL" }, {(char)0x08, "BS"  },
            {(char)0x09, "HT"  }, {(char)0x0A, "LF"  }, {(char)0x0B, "VT"  },
            {(char)0x0C, "FF"  }, {(char)0x0D, "CR"  }, {(char)0x0E, "SO"  },
            {(char)0x0F, "SI"  }, {(char)0x10, "DLE" }, {(char)0x11, "DC1" },
            {(char)0x12, "DC2" }, {(char)0x13, "DC3" }, {(char)0x14, "DC4" },
            {(char)0x15, "NAK" }, {(char)0x16, "SYN" }, {(char)0x17, "ETB" },
            {(char)0x18, "CAN" }, {(char)0x19, "EM"  }, {(char)0x1A, "SUB" },
            {(char)0x1B, "ESC" }, {(char)0x1C, "FS"  }, {(char)0x1D, "GS"  },
            {(char)0x1E, "RS"  }, {(char)0x1F, "US"  }, {(char)0x7F, "DEL" },
        };

        private void FileViewerForm_Load(object sender, EventArgs e)
        {

        }
    }
    public static class RichTextBoxExtensions
    {
        public static void AppendText(this RichTextBox box, string text, System.Drawing.Color bgcolor, Color fontcolor)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            var saved = box.SelectionBackColor;
            var saved2 = box.SelectionColor;
            box.SelectionBackColor = bgcolor;
            box.SelectionColor = fontcolor;
            box.AppendText(text);
            box.SelectionBackColor = saved;
            box.SelectionColor = saved2;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统图;
使用System.IO;
使用System.Windows.Forms;
命名空间文件查看器
{
公共部分类FileViewPerform:表单
{
公共文件ViewPerform()
{
初始化组件();