事先要在本地创建一个叫该名称的文件,在代码路径下。TempFile.txt
你可以自由的输入一些换行符,或者复制一片文章进去,然后运行代码就会发现有行号。
using System;
class Program
{
static void Main(string[] args)
{
string FilePath = "TempFile.txt";
string[] lines=File.ReadAllLines(FilePath);
string[] newLines=new string[lines.Length];
newLines[0]=lines[0];
for(int i = 1; i < lines.Length; i++)
{
newLines[i] = $"{i}\t{lines[i]}";
}
File.WriteAllLines(FilePath, newLines);
Console.WriteLine("已完成");
Console.ReadLine();
}
}
运行结果: