4 ответов:
string[] fileLines = File.ReadAllLines(@"your file path"); var result = fileLines.Skip(4).Take(fileLines.Length - (4 + 6)); File.WriteAllLines(@"your output file path", result);
Не кажется, что это самый короткий путь... но для меня это работает... Надеюсь, что это дает некоторое представление.
System.IO.StreamReader input = new System.IO.StreamReader(@"originalFile.txt"); System.IO.StreamWriter output = new System.IO.StreamWriter(@"outputFile.txt"); String[] allLines = input.ReadToEnd().Split("\n".ToCharArray()); int numOfLines = allLines.Length; int lastLineWeWant = numOfLines - (6); //The last index we want. for (int x = 0; x < numOfLines; x++) { if (x > 4 - 1 && x < lastLineWeWant) //Index has to be greater than num to skip @ start and below the total length - num to skip at end. { output.WriteLine(allLines[x].Trim()); //Trim to remove any \r characters. } } input.Close(); output.Close();
StreamReader.ReadLine()считывает файл строка за строкой, и вы можете построить массив строк из файла. Затем удалите из массива первые четыре и последние 6 строк. И с помощьюStreamWriter.WriteLine()Вы можете заполнить новый файл строка за строкой, беря из вашего массива. Должно быть совсем просто.
Вот самый простой способ сделать это в VB.NET:
Private Sub ReplaceString() Dim AllLines() As String = File.ReadAllLines("c:\test\myfile.txt") For i As Integer = 0 To AllLines.Length - 1 If AllLines(i).Contains("foo") Then AllLines(i) = AllLines(i).Replace("foo", "boo") End If Next File.WriteAllLines("c:\test\myfile.txt", AllLines) End Sub
Comments