从文本文件中删除文本c#

时间:2015-02-27 12:07:41

标签: c# text listbox

我有一些代码需要从文本文件中删除并存储在列表框中 这是代码:

   Version=106
Monitor=34
SMode=111111100
Date=20130205
StartTime=15:46:20.0
Length=01:06:18.9
Interval=1
Upper1=0
Lower1=0
Upper2=0
Lower2=0
Upper3=180
Lower3=170
Timer1=00:00:00.0
Timer2=00:00:00.0
Timer3=00:00:00.0
ActiveLimit=0
MaxHR=195
RestHR=46
StartDelay=0
VO2max=48
Weight=66

[Note]

[IntTimes]
00:09:59.0  117 91  127 153
0   0   0   124 41  309
0   0   0   0   0
0   2181    68  0   0   0   
0   0   0   0   0   0
00:10:00.0  115 91  127 153
0   0   0   119 0   309
0   0   0   0   0
33554432    2184    0   0   0   0
500 0   0   0   0   0
00:20:10.0  173 109 161 177
0   0   0   112 62  307
0   0   0   0   0
0   3461    22  0   0   0
0   0   0   0   0   0
00:25:02.0  119 111 126 172
0   0   0   190 54  308
0   0   0   0   0 
0   764 133 0   0   0
0   0   0   0   0   0
00:35:16.0  174 119 167 179
0   0   0   121 52  309
0   0   0   0   0
0   3500    70  0   0   0
0   0   0   0   0   0
00:50:00.0  134 109 153 179
0   0   0   178 0   310 
0   0   0   0   0
33554432    10840   0   0   0   0
500 0   0   0   0   0
01:00:00.0  150 112 139 175
0   0   0   66  0   312
0   0   0   0   0
33554432    1741    0   0   0   0
500 0   0   0   0   0
01:06:18.9  108 105 139 177
0   0   0   64  123 313
0   0   0   0   0
0   5668    0   80  0   0
0   0   0   0   0   0

[IntNotes]
1   
2   
3   
4   
5   
6   
7   
8   

 [ExtraData]

[LapNames]
1   1   
2   4   
3   1   
4   1   
5   1   
6   4   c
7   4   
8   1   

[Summary-123]
3978    0   3978    0   0   0
195 0   0   46
3978    0   3978    0   0   0
195 0   0   46
0   0   0   0   0   0
195 0   0   46
0   3978

[Summary-TH]
3978    0   0   588 3390    0
195 180 170 46
0   3978

[HRZones]
190
175
162
152
143
133
0
0
0
0
0

[SwapTimes]

[Trip]
250
0
3978
309
313
229
504
651
//n header

[HRData] 

我需要从以下位置删除数据: Upper1 = 0 至 [HRDATA] 并将其存储在我的listbox1中,以便它不会显示在我的datagridview

我尝试过使用topindex功能,但无济于事 我收到了错误

感谢帮助

2 个答案:

答案 0 :(得分:2)

正如Alex所写,一个非常简单的代码是:

string[] lines = File.ReadLines("TextFile1.txt")
                     .SkipWhile(x => x != "Upper1=0")
                     .TakeWhile(x => x != "[HRData]")
                     .ToArray();

使用StreamReader它有点复杂(但不是那么多)

List<string> lines = new List<string>();
bool skipping = true;

using (var sr = new StreamReader("TextFile1.txt")) {
    string line;
    while ((line = sr.ReadLine()) != null) {
        line = line.TrimEnd();

        if (skipping) {
            if (line == "Upper1=0") {
                skipping = false;
            }
        }

        if (!skipping) {
            lines.Add(line);

            if (line == "[HRData]") {
                break;
            }
        }
    }
}

现在由SkipWhile / TakeWhile完成之前的事情由标记skipping

完成

(在这两种情况下,Upper1=0[HRData]都在集合中获取)

这是StreamReader版本的一个小变体。它实现了一个非常简单的状态机(我知道,我很懒,我应该使用enum而不是int)来分割“之前”,“中间”之间的界限“和”之后“。

List<string> linesBefore = new List<string>();
List<string> lines = new List<string>();
List<string> linesAfter = new List<string>();

// 0 = before Upper1=0, 
// 1 = between Upper1=0 and [HRData]
// 2 = after [HRData]
int state = 0;

using (var sr = new StreamReader("TextFile1.txt")) {
    string line;
    while ((line = sr.ReadLine()) != null) {
        line = line.TrimEnd();

        if (state == 0) {
            if (line == "Upper1=0") {
                state = 1;
                lines.Add(line);
            } else {
                linesBefore.Add(line);
            }
        } else if (state == 1) {
            lines.Add(line);

            if (line == "[HRData]") {
                state = 2;
            }
        } else {
            // state == 2
            linesAfter.Add(line);
        }
    }
}

答案 1 :(得分:0)

这个例子

Regex.Replace(File.ReadAllText(@"C:\MyIniFile.txt"),  // Read all lines into buffer.
               @"(Upper1=0)(.*)(\[HRData\])",         // Set upper to HRData to be removed
               string.Empty,                          // 'replace' the section with a empty line
               RegexOptions.Singleline)
      .Split(new[] { "\r\n", "\r", "\n" },           // Split the remaining buffer into lines
             StringSplitOptions.RemoveEmptyEntries); // Don't show empty lines.
  1. 从文件中将所有行读入缓冲区。
  2. 使用正则表达式替换删除所需的部分。
  3. 在删除任何空白行的同时将剩余的行拆分为多个行(​​如果要显示空白,请将其删除)。