C#File.ReadAllLines不在换行符上中断
我正在构建一个需要修改配置文件的应用程序 我的问题是我无法逐行读取文件。我一直将整个文件作为单个字符串进行定位C#File.ReadAllLines不在换行符上中断,c#,.net,file-io,C#,.net,File Io,我正在构建一个需要修改配置文件的应用程序 我的问题是我无法逐行读取文件。我一直将整个文件作为单个字符串进行定位 string ConfigTemplate = AEBuildsSPFolderName + "\\Template_BuildReleaseScript.Config"; string[] fileSourceLines = File.ReadAllLines(ConfigTemplate, Encoding.Default); //Returns the entire file
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.
知道我做错了什么吗
谢谢
david您的文件可能使用
\n
字符(没有\r
)作为换行符。我猜使用的换行符可能不是\r\n
将整个文件读入单个字符串时,请尝试调用yourString.Split(新字符[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries)
并查看这是否适用于您
另外,由于
ReadAllLines()
只是读取单个字符串,因此您可以简单地使用ReadAllText()
谢谢您的回复。根据您的评论,这里是我整理的一段测试代码//获取文件行字符串[]fileSourceLines=File.ReadAllLines(ConfigTemplate,Encoding.Default);如果(fileSourceLines.Length==1){string yourString=fileSourceLines[0];string[]newSourceLines=yourString.Split(新字符[]{'\r','\n'},StringSplitOptions.removeMptyEntries);//此数组仍然只包含1个字符串。}newSourceLines数组仍然只包含1个字符串。任何其他建议:)谢谢,大卫