Title say it all - I'm trying to remove every line from a file that contains the string PresenceManager I first loop through the file, removing any instance of the string. However, this leaves a TON of empty lines, so I have to go and remove those with another block of code. This seems like quite a lot of code to do such a seemingly easy task. Is their any way to shorten/optimize this? public static void checkDirtyLogs(Skype s, Form1 f1)    {        foreach (string log in Directory.GetFiles(f1.path, debug*.log))        {            string[] currentFile = File.ReadAllLines(log);            for (int i = 0; i < currentFile.Length - 1; i++)            {                if (!currentFile[i].Contains(PresenceManager))                {                    currentFile[i] = ;                }            }            File.WriteAllLines(log, currentFile);            var tempFileName = Path.GetTempFileName();            try            {                using (var streamReader = new StreamReader(log))                using (var streamWriter = new StreamWriter(tempFileName))                {                    string line;                    while ((line = streamReader.ReadLine()) != null)                    {                        if (!string.IsNullOrWhiteSpace(line))                            streamWriter.WriteLine(line);                    }                }                File.Copy(tempFileName, log, true);            }            finally            {                File.Delete(tempFileName);            }        }    } //Clean logs
Is This An Efficient Way To Remove All Lines From A File That Do Not Contain A Certain String?
c#
1) Create two streams, one with the input and one with the output:using(var reader = new StreamReader(log))using(var writer = new StreamWriter(tempFile))2) Read each line from the reader, check it and write to the writer if if passeswhile((line = reader.ReadLine()) != null){     if(!line.Contains(PresenceManager))     {         writer.WriteLine(line);     }}3) Delete the log file and replace it with the temp file.Or check out this powershell answer for a one liner.