Inheritance: System.EventArgs
Exemplo n.º 1
0
        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
                        sw.Write(line);

                        if (i < Count - 1)
                        {
                            sw.Write(SaveEOL);
                        }

                        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;
        }
Exemplo n.º 2
0
        public override void SaveToFile(string fileName, Encoding enc)
        {
            //
            var newLinePos = new List<int>(Count);
            //create temp file
            var dir = Path.GetDirectoryName(fileName);
            if (dir != null) {
                var tempFileName = Path.Combine(dir, Path.GetFileNameWithoutExtension(fileName) + ".tmp");
                FileStream tempFs = null;
                StreamReader sr = new StreamReader(fs, fileEncoding);
                try {
                    tempFs = new FileStream(tempFileName, FileMode.Create);
                    using (StreamWriter sw = new StreamWriter(tempFs, enc)) {
                        int fsLen = (int)tempFs.Length;
                        tempFs = null;
                        sw.Flush();

                        for (int i = 0; i < Count; i++) {
                            newLinePos.Add(fsLen);

                            var sourceLine = ReadLine(sr, i);//read line from source file

                            bool lineIsChanged = lines[i] != null && lines[i].IsChanged;

                            var line = lineIsChanged ? lines[i].Text : 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
                            sw.Write(line);

                            if (i < Count - 1)
                                sw.Write(SaveEOL);

                            sw.Flush();
                        }
                    }
                } finally {
                    tempFs?.Dispose();
                }

                //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);
            fileEncoding = enc;
        }