Kyle
Kyle

Reputation: 11

Keeping only a certain line and deleting the rest in C#

I was wondering if anyone could give me a quick example of how to do this. What I want to do is parse a .txt file and delete everything but lines containing "Type: Whisper", is there any way to achieve this with relative ease?

Upvotes: 1

Views: 106

Answers (2)

Zenwalker
Zenwalker

Reputation: 1919

Do Readline from 1 file and then do String.Contains over that line for "Type: Whisper" text, if you get it, jst skip. If not, just write that line to new file and delete the old file at the end.

Upvotes: 0

alun
alun

Reputation: 3441

You don't give alot of detail but something along these lines will overwrite the file with a new one with those lines removed:

        var fileName = @"c:\temp\myFile.txt";
        System.IO.File.WriteAllLines(
            fileName, 
            System.IO.File.ReadAllLines(fileName)
                .Where(x => !x.Contains("Type: Whisper")).ToArray());

Upvotes: 2

Related Questions