/// <summary> /// Appends the file with lines not already contained in the file, from the specified line list /// </summary> /// <param name="lineList">The line list.</param> /// <param name="altPath">The alt path -- if specified it will create new file and will return operator for it</param> public fileTextOperater Append(IEnumerable <String> lineList, String altPath = "") { String tmpPath = filePath + "bck"; if (!altPath.isNullOrEmpty()) { tmpPath = altPath; } FileInfo tmpFile = tmpPath.getWritableFile(getWritableFileMode.overwrite); List <String> linesToAppend = lineList.ToList(); Int32 ln = 0; using (var st = File.OpenText(file.FullName)) { var ost = File.AppendText(tmpFile.FullName); while (!st.EndOfStream) { String line = st.ReadLine(); if (lineList.Contains(line)) { linesToAppend.Remove(line); } ost.WriteLine(line); ln++; } foreach (String apln in linesToAppend) { ost.WriteLine(apln); ln++; } ost.Close(); ost.Dispose(); st.Close(); st.Dispose(); } if (altPath.isNullOrEmpty()) { File.Copy(tmpFile.FullName, file.FullName, true); File.Delete(tmpFile.FullName); estLinesInFile = Math.Max(estLinesInFile, ln); return(this); } else { fileTextOperater op = new fileTextOperater(tmpFile.FullName); op.estLinesInFile = ln; return(op); } }
/// <summary> /// Removes all lines matching the lineList /// </summary> /// <param name="lineList">the list of lines to remove</param> /// <param name="altPath">If specified output is written to the path, original file is not changed</param> public fileTextOperater Remove(List <String> lineList, String altPath = "") { String tmpPath = filePath + "bck"; if (!altPath.isNullOrEmpty()) { tmpPath = altPath; } FileInfo tmpFile = tmpPath.getWritableFile(getWritableFileMode.overwrite); Int32 ln = 0; Int32 rm = 0; using (var st = File.OpenText(file.FullName)) { var ost = File.AppendText(tmpFile.FullName); while (!st.EndOfStream) { String line = st.ReadLine(); if (!lineList.Contains(line)) { ost.WriteLine(line); } else { rm++; } ln++; } ost.Close(); ost.Dispose(); st.Close(); st.Dispose(); } memMap = null; if (altPath.isNullOrEmpty()) { File.Copy(tmpFile.FullName, file.FullName, true); File.Delete(tmpFile.FullName); estLinesInFile = ln - rm; return(this); } else { fileTextOperater op = new fileTextOperater(tmpFile.FullName); op.estLinesInFile = ln - rm; return(op); } }