public override void SaveToFile(string fileName, Encoding enc) { // var newLinePos = new List<int>(Count); //create temp file var dir = Path.GetDirectoryName(fileName); var tempFileName = Path.Combine(dir, Path.GetFileNameWithoutExtension(fileName) + ".tmp"); StreamReader sr = new StreamReader(fs, fileEncoding); using (FileStream tempFs = new FileStream(tempFileName, FileMode.Create)) using (StreamWriter sw = new StreamWriter(tempFs, enc)) { sw.Flush(); for (int i = 0; i < Count; i++) { newLinePos.Add((int)tempFs.Length); var sourceLine = ReadLine(sr, i);//read line from source file string line; bool lineIsChanged = lines[i] != null && lines[i].IsChanged; if (lineIsChanged) line = lines[i].Text; else line = sourceLine; //call event handler if (LinePushed != null) { var args = new LinePushedEventArgs(sourceLine, i, lineIsChanged ? line : null); LinePushed(this, args); if(args.SavedText != null) line = args.SavedText; } //save line to file if (i == Count - 1) sw.Write(line); else sw.WriteLine(line); sw.Flush(); } } //clear lines buffer for (int i = 0; i < Count; i++) lines[i] = null; //deattach from source file sr.Dispose(); fs.Dispose(); //delete target file if (File.Exists(fileName)) File.Delete(fileName); //rename temp file File.Move(tempFileName, fileName); //binding to new file sourceFileLinePositions = newLinePos; fs = new FileStream(fileName, FileMode.Open); this.fileEncoding = enc; }
public override void SaveToFile(string fileName, Encoding enc) { // var newLinePos = new List <int>(Count); //create temp file var dir = Path.GetDirectoryName(fileName); var tempFileName = Path.Combine(dir, Path.GetFileNameWithoutExtension(fileName) + ".tmp"); StreamReader sr = new StreamReader(fs, fileEncoding); using (FileStream tempFs = new FileStream(tempFileName, FileMode.Create)) using (StreamWriter sw = new StreamWriter(tempFs, enc)) { sw.Flush(); for (int i = 0; i < Count; i++) { newLinePos.Add((int)tempFs.Length); var sourceLine = ReadLine(sr, i);//read line from source file string line; bool lineIsChanged = lines[i] != null && lines[i].IsChanged; if (lineIsChanged) { line = lines[i].Text; } else { line = sourceLine; } //call event handler if (LinePushed != null) { var args = new LinePushedEventArgs(sourceLine, i, lineIsChanged ? line : null); LinePushed(this, args); if (args.SavedText != null) { line = args.SavedText; } } //save line to file if (i == Count - 1) { sw.Write(line); } else { sw.WriteLine(line); } sw.Flush(); } } //clear lines buffer for (int i = 0; i < Count; i++) { lines[i] = null; } //deattach from source file sr.Dispose(); fs.Dispose(); //delete target file if (File.Exists(fileName)) { File.Delete(fileName); } //rename temp file File.Move(tempFileName, fileName); //binding to new file sourceFileLinePositions = newLinePos; fs = new FileStream(fileName, FileMode.Open); this.fileEncoding = enc; }