将纯文本文件导入C#中的控件 [英] A plain text file into a control in C#

查看:111
本文介绍了将纯文本文件导入C#中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,这是我的新挑战:

我在纯文本文件中有一些信息.我想恢复它并使用C#将其发布给用户.该文本包含一些转义命令,例如\ n或\ t.我尝试使用RichTextBox,但不起作用:thumbsdown :.

有什么方法可以做到吗?我知道有,但是我找不到:(

Hey there, this is my new challenge:

I have some info in a plain text file. I want to recover it and publish it to the user using C#. This text has some escape commands such as \n or \t. I tried with a RichTextBox and it doesn''t work :thumbsdown:.

Is there any method to do it? I know there is, but I can''t find it :(

推荐答案

如果您想摆脱所有这些转义字符,则可以使用建议的方法在来自MSDN.Social的该线程中 [ ^ ].
If you want to get rid of all these escaped characters then you could use the method suggested in this thread from MSDN.Social[^].


在表单上创建一个文本框(假设您正在使用Windows窗体),然后使用以下代码:
Create a Textbox on your form (assuming you are using Windows Forms), then use this code:
textBox1.Multiline = true;
textBox1.Dock = DockStyle.Fill;
textBox1.Text = "\r\nHello\r\n\r\n\tHow are you?";


在Windows中,新行用"\ r \ n"表示,而不仅仅是"\ n".如果您的文件包含"\ n"字符,则可以使用正则表达式或String.Replace修复要显示的字符:


In Windows, a new line is represented by "\r\n" rather than just "\n". If you have a file that contains "\n" characters, you can use a regular expression or String.Replace to fix the characters for display:

textBox1.Text = "\nHello\n\n\tHow are you?".Replace("\n", "\r\n");


或:


Or:

textBox1.Text = System.Text.RegularExpressions.Regex.Replace("\nHello\n\n\tHow are you?", "(?<!\r)\n", "\r\n");



删除了我在此处忘记的一些测试代码.



Removed some testing code I forgot in there.


这篇关于将纯文本文件导入C#中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆