C#我怎样才能读取txt文件换行符 [英] C# how can i read txt files newlines

查看:432
本文介绍了C#我怎样才能读取txt文件换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



i write ac#program读取txt文件并打印出来。

我的文本文件如:

Hello,

i write a c# program which read txt files and print it.
my text files like:

<br />
Hi\r\nMy Name is





但是当我尝试打印时,我看不到如下结果:



but when i try to print i can't see the result like:

Hi
My Name is





如何解决这个问题呢?



我尝试过的事情:



i尝试删除 \ r,\ n 并查看网页

推荐答案

string contents = File.ReadAllText(@"C:\test.txt");
            MessageBox.Show(contents);


请阅读我对这个问题的评论。我认为你的档案坏了。 \ r \ n 开关是不可打印的标志,不应该是可见的。

请参阅:字符串(C#编程指南) [ ^ ]





检查:

Please, read my comments to the question. I think your file is broken. \r and \n switches are non-printable signs and shouldn't be visible.
See: Strings (C# Programming Guide)[^]


Check this:
string s = "Hi\r\nMy name is Maciej";

File.WriteAllText(@"D:\s.txt",s);

s = File.ReadAllText(@"D:\s.txt");
Console.WriteLine(s);





打印:



It prints:

Hi
My name is Maciej


这篇关于C#我怎样才能读取txt文件换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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