我正在构建一个需要修改配置文件的应用程序。
我的问题是我无法逐行读取文件。我一直把整个文件作为一个字符串。
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.
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
谢谢,
大卫
小智 6
我猜使用的换行符可能不是 \r\n
当您将整个文件读入单个字符串时,请尝试调用yourString.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
并查看它是否适合您。
此外,由于ReadAllLines()
无论如何都只是读入一个字符串,因此您可以简单地使用ReadAllText()
.