从文本文件中删除文本 C#
Remove text from a textfile c#
我有一些代码需要从文本文件中删除并存储在列表框中
这是代码:
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]
我需要从以下位置删除数据:
上1 = 0
至
[人力资源数据]
并将其存储在我的 listbox1 中,这样它就不会显示在我的数据网格视图中
我试过使用 topindex 函数但没有用
我收到错误
感谢帮助谢谢
正如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
)来拆分 "before"、 "middle" 和 "after".
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);
}
}
}
这个例子
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.
- 从文件中将所有行读入缓冲区。
- 使用正则表达式替换删除所需的部分。
- 将剩余的行拆分为一个行数组,同时删除所有空白行(如果要显示空白,请删除)。
我有一些代码需要从文本文件中删除并存储在列表框中 这是代码:
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]
我需要从以下位置删除数据: 上1 = 0 至 [人力资源数据] 并将其存储在我的 listbox1 中,这样它就不会显示在我的数据网格视图中
我试过使用 topindex 函数但没有用 我收到错误
感谢帮助谢谢
正如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
)来拆分 "before"、 "middle" 和 "after".
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);
}
}
}
这个例子
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.
- 从文件中将所有行读入缓冲区。
- 使用正则表达式替换删除所需的部分。
- 将剩余的行拆分为一个行数组,同时删除所有空白行(如果要显示空白,请删除)。