C#File.ReadAllLines在换行符上没有中断 [英] C# File.ReadAllLines not breaking on line feeds

查看:167
本文介绍了C#File.ReadAllLines在换行符上没有中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在构建的应用程序,需要修改配置文件.

I have an application that I am building that needs to modify a configuration file.

我的问题是我无法逐行读取文件.我一直将整个文件作为一个字符串保存.

My problem is that I am not able to read the file in line by line. I keep geeting the the entire file as a single string.

string ConfigTemplate = AEBuildsSPFolderName + "\\Template_BuildReleaseScript.Config";

string[] fileSourceLines = File.ReadAllLines(ConfigTemplate, Encoding.Default);
//Returns the entire file contents into the first array element.

using (StreamReader reader = new StreamReader(ConfigTemplate))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
//Returns the entire file contents into the first line read.

有什么想法我做错了吗?

Any idea of what I am doing wrong?

谢谢

大卫

推荐答案

我猜测使用的换行符可能不是\r\n

I'm guessing the line break character used might not be \r\n

当您将整个文件读为一个字符串时,请尝试调用yourString.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);,看看是否适合您.

When you read your entire file into a single string, try calling yourString.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); and see if that works for you.

此外,由于ReadAllLines()仍只是读入单个字符串,因此您可以简单地使用ReadAllText().

Also since ReadAllLines() was just reading into a single string anyway, you could simply use ReadAllText().

这篇关于C#File.ReadAllLines在换行符上没有中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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