How do I move items/values up and down a text file. At the moment my program reads a text file, an uses a while to make sure it stop when there is no more lines to read. I used an if statement to check if counter equals the line of the value I want to move. I am stuck not sure how to continue from here.
如何上下移动文本文件中的项/值。当我的程序读取一个文本文件时,an会用一段时间来确保当没有更多的行可以读取时它会停止。我使用if语句检查counter是否等于我要移动的值的行。我不知道该如何从这里继续下去。
_upORDown = 1;
using (StreamReader reader = new StreamReader("textfile.txt"))
{
string line = reader.ReadLine();
int Counter = 1;
while (line != null)
{
if (Counter == _upORDown)
{
//Remove item/replace position
}
Counter++;
}
}
2 个解决方案
#1
3
You can read the file in memory, move the line to where you need it, and write the file back. You can use ReadAllLines
and WriteAllLines
.
您可以在内存中读取文件,将该行移动到需要它的位置,并将文件写回。您可以使用ReadAllLines和WriteAllLines。
This code moves the string at position i
up by one line:
此代码将字符串在位置i上移动一行:
if (i == 0) return; // Cannot move up line 0
string path = "c:\\temp\\myfile.txt";
// get the lines
string[] lines = File.ReadAllLines(path);
if (lines.Length <= i) return; // You need at least i lines
// Move the line i up by one
string tmp = lines[i];
lines[i] = lines[i-1];
lines[i-1] = tmp;
// Write the file back
File.WriteAllLines(path, lines);
#2
0
@dasblinkenlight's answer, using LINQ:
使用LINQ @dasblinkenlight的回答:
string path = "c:\\temp\\myfile.txt";
var lines = File.ReadAllLines(path);
File.WriteAllLines(
path,
lines.Take(i).Concat(
lines.Skip(i+1)
)
);
This deletes the line at position i
(zero-based) and moves the other lines up.
这将删除位置i(基于零的)的行,并将其他行向上移动。
Adding to a new line:
增加一条新线路:
string path = "c:\\temp\\myfile.txt";
var lines = File.ReadAllLines(path);
var newline = "New line here";
File.WriteAllLines(
path,
lines.Take(i).Concat(
new [] {newline}
).Concat(
lines.Skip(i+1)
)
);
#1
3
You can read the file in memory, move the line to where you need it, and write the file back. You can use ReadAllLines
and WriteAllLines
.
您可以在内存中读取文件,将该行移动到需要它的位置,并将文件写回。您可以使用ReadAllLines和WriteAllLines。
This code moves the string at position i
up by one line:
此代码将字符串在位置i上移动一行:
if (i == 0) return; // Cannot move up line 0
string path = "c:\\temp\\myfile.txt";
// get the lines
string[] lines = File.ReadAllLines(path);
if (lines.Length <= i) return; // You need at least i lines
// Move the line i up by one
string tmp = lines[i];
lines[i] = lines[i-1];
lines[i-1] = tmp;
// Write the file back
File.WriteAllLines(path, lines);
#2
0
@dasblinkenlight's answer, using LINQ:
使用LINQ @dasblinkenlight的回答:
string path = "c:\\temp\\myfile.txt";
var lines = File.ReadAllLines(path);
File.WriteAllLines(
path,
lines.Take(i).Concat(
lines.Skip(i+1)
)
);
This deletes the line at position i
(zero-based) and moves the other lines up.
这将删除位置i(基于零的)的行,并将其他行向上移动。
Adding to a new line:
增加一条新线路:
string path = "c:\\temp\\myfile.txt";
var lines = File.ReadAllLines(path);
var newline = "New line here";
File.WriteAllLines(
path,
lines.Take(i).Concat(
new [] {newline}
).Concat(
lines.Skip(i+1)
)
);