简体   繁体   English

一次读取一行文本文件

[英]Reading text file one line at a time

I have got a text file with a number of commands to parse through my system. 我有一个文本文件,其中包含许多要通过系统解析的命令。 Each line within the text file is a command. 文本文件中的每一行都是一个命令。

I would like to read the text file, one line at a time, where each line would be a command for me to act on. 我想一次读取一行文本文件,其中每一行都是我要执行的命令。

I have reached a state where some lines are read completely by using StreamReader.ReadLine() , however some lines are not read completely. 我已经达到通过使用StreamReader.ReadLine()完全读取某些行的状态,但是有些行没有完全读取。 Within notepad, there appears to be nothing wrong with the commands, and each one appears in one line like it is supposed to. 在记事本中,命令似乎没有问题,并且每个命令都按预期的样子排成一行。

However, when opening the file with notepad++, I notice LF symbols within some commands, which instructs the text editor to start a new command, like so: 但是,当使用notepad ++打开文件时,我注意到某些命令中包含LF符号,这些命令指示文本编辑器启动一个新命令,如下所示:

在此处输入图片说明

Those LF are misplaced (ie I do not want to start a new line when the LF is part of a command). 这些LF放错了位置(即,当LF是命令的一部分时,我不想开始新行)。 Is there any way to make the StreamReader interpret the file like Notepad? 有什么方法可以使StreamReader像记事本一样解释文件? As there are no line breaks in the middle of commands within Notepad. 由于记事本中的命令中间没有换行符。

If all text is one line then you could skip using StreamReader . 如果所有文本都是一行,则可以使用StreamReader跳过。 Instead you can use 相反,您可以使用

string sLine = File.ReadAllText("test.txt");
sLine = sLine.Replace("\n", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM