Вы можете сделать следующее:
- Считайте количество нужных вам строк.
- Удалить файл.
- Запишите свои строки во вновь созданный файл с тем же путем и именем.
Ниже приведен пример кода:
using System.IO;
using System.Collections.Generic;
namespace ConsoleApp3
{
public class Program
{
static void Main(string[] args)
{
int lineNumber = 4;
string filePath = @"C:\Users\Admin\Desktop\test - Copy.txt";
List<string> lines = new List<string>();
using (TextReader tr = new StreamReader(filePath))
{
int i = 0;
while(i!=lineNumber)
{
lines.Add(tr.ReadLine());
i++;
}
}
File.Delete(filePath);
File.WriteAllLines(filePath,lines);
}
}
}