Пример #1
0
        private void UndoChangesInFile(FileModification fm)
        {
            List <string> fileToString = new List <string>();

            if (ConvertFileToListString(fm.FileName, out fileToString))
            {
                foreach (var item in fm.Changes)
                {
                    StringModification sm = (StringModification)item;
                    fileToString[sm.NumberOfString] = sm.OldString;
                }
                try
                {
                    using (var undoFile = new StreamWriter(fm.FileName))
                    {
                        foreach (var item in fileToString)
                        {
                            undoFile.WriteLine(item);
                        }
                    }
                } catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                    Console.WriteLine("Can't undo changes in file {0} because of error in file", fm.FileName);
                }
            }
            else
            {
                Console.WriteLine("Can't undo changes in file {0} because of error in file", fm.FileName);
            }
        }
Пример #2
0
        private FileModification FmCompareFiles(string newFileName, string oldFileName)
        {
            var fm = new FileModification()
            {
                Data = DateTime.Now.Ticks, FileName = newFileName, ModType = ModifType.Changed
            };

            fm.Changes = new List <StringModification>();
            List <string> newFileToListString;
            List <string> oldFileToListString;

            ConvertFileToListString(newFileName, out newFileToListString);
            ConvertFileToListString(oldFileName, out oldFileToListString);
            for (int i = 0; i < oldFileToListString.Count; i++)
            {
                if (i >= newFileToListString.Count)
                {
                    fm.Changes.Add(new StringModification(i, oldFileToListString[i], ""));
                }
                else if (newFileToListString[i] != oldFileToListString[i])
                {
                    fm.Changes.Add(new StringModification(i, oldFileToListString[i], newFileToListString[i]));
                }
            }
            if (newFileToListString.Count > oldFileToListString.Count)
            {
                for (int i = oldFileToListString.Count; i < newFileToListString.Count; i++)
                {
                    fm.Changes.Add(new StringModification(i, "", newFileToListString[i]));
                }
            }

            Console.WriteLine(fm.FileName);
            Console.WriteLine(fm.Data);
            foreach (var item in fm.Changes)
            {
                if (item is StringModification)
                {
                    StringModification sm = (StringModification)item;
                    Console.WriteLine("{0} : {1} => {2}", sm.NumberOfString, sm.OldString, sm.NewString);
                }
            }
            return(fm);
        }