c#文件。ReadAllLines不会在换行时中断

时间:2022-07-20 13:26:59

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?

知道我做错了什么吗?

Thanks,

谢谢,

david

大卫

2 个解决方案

#1


6  

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

我猜使用的断行字符可能不是\r\n

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.

当您将整个文件读入一个字符串时,尝试调用您的字符串。拆分(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);看看这对你是否有用。

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

此外,由于ReadAllLines()只是读取单个字符串,所以您可以简单地使用ReadAllText()。

#2


1  

Your file probably uses \n characters (without \r) as newlines.

您的文件可能使用\n个字符(没有\r)作为换行符。

#1


6  

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

我猜使用的断行字符可能不是\r\n

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.

当您将整个文件读入一个字符串时,尝试调用您的字符串。拆分(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);看看这对你是否有用。

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

此外,由于ReadAllLines()只是读取单个字符串,所以您可以简单地使用ReadAllText()。

#2


1  

Your file probably uses \n characters (without \r) as newlines.

您的文件可能使用\n个字符(没有\r)作为换行符。