Пример #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 FmDeleteFile(string fileName)
        {
            var fm = new FileModification()
            {
                Data = DateTime.Now.Ticks, FileName = fileName, ModType = ModifType.Deleted
            };

            File.Copy(fileName + ".tmp", fileName + ".del", true);
            return(fm);
        }
Пример #3
0
        public void UndoToData(long data)
        {
            List <string> logToString = new List <string>();

            if (ConvertFileToListString(directoryPath + @"\logg", out logToString))
            {
                for (int i = logToString.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        FileModification fm = JsonConvert.DeserializeObject <FileModification>(logToString[i]);
                        if (fm != null && fm.Data > data)
                        {
                            switch (fm.ModType)
                            {
                            case ModifType.Changed:
                            {
                                UndoChangesInFile(fm);
                                break;
                            }

                            case ModifType.Created:
                            {
                                File.Delete(fm.FileName);
                                break;
                            }

                            case ModifType.Renamed:
                            {
                                File.Move(fm.NewFileName, fm.FileName);
                                break;
                            }

                            case ModifType.Deleted:
                            {
                                File.Move(fm.FileName + ".del", fm.FileName);
                                break;
                            }

                            default:
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine(ex.Message);
                        Console.WriteLine("Can't undo changes in file because of internal error");
                    }
                }
            }

            File.Delete(directoryPath + @"\logg");
        }
Пример #4
0
        private void OnRenamed(object source, RenamedEventArgs e)
        {
            WatcherChangeTypes changeTypes = e.ChangeType;

            Console.WriteLine("File {0} {1} to {2}", e.OldFullPath, e.FullPath, changeTypes.ToString());

            var fm = new FileModification()
            {
                Data    = DateTime.Now.Ticks, FileName = e.OldFullPath, NewFileName = e.FullPath,
                ModType = ModifType.Renamed
            };

            Log(JsonConvert.SerializeObject(fm));

            File.Copy(e.FullPath, e.FullPath + ".tmp", true);
            File.Delete(e.OldFullPath + ".tmp");
        }
Пример #5
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);
        }