Пример #1
0
        public override void OnChanged(object sender, FileSystemEventArgs e)
        {
            try
            {
                Watcher.EnableRaisingEvents = false;
                Console.WriteLine("Файл изменен " + e.Name);
                string text = File.ReadAllText(RootFolder + "/" + e.Name, Encoding.Default);

                if (!File.Exists(RootFolder + "/" + e.Name + IndexFormat))
                {
                    File.Create(RootFolder + "/" + e.Name + IndexFormat).Close();
                    DataTxt dataTxt = new DataTxt(CountWords(text));
                    File.WriteAllText(RootFolder + "/" + e.Name + IndexFormat, JsonConvert.SerializeObject(dataTxt));
                }
                else
                {
                    DataTxt dataTxt = JsonConvert.DeserializeObject <DataTxt>(File.ReadAllText(RootFolder + "/" + e.Name + IndexFormat));
                    dataTxt.words = CountWords(text);
                    File.WriteAllText(RootFolder + "/" + e.Name + IndexFormat, JsonConvert.SerializeObject(dataTxt));
                }
            }
            finally
            {
                Watcher.EnableRaisingEvents = true;
            }
        }
Пример #2
0
        public void RunIndexTxt()
        {
            foreach (string file in Directory.EnumerateFiles(RootFolder, "*.txt", SearchOption.AllDirectories))
            {
                string text = File.ReadAllText(file, Encoding.Default);

                if (!File.Exists(file + IndexFormat))
                {
                    File.Create(file + IndexFormat).Close();
                    DataTxt dataTxt = new DataTxt(CountWords(text));
                    File.WriteAllText(file + IndexFormat, JsonConvert.SerializeObject(dataTxt));
                }
                else
                {
                    DataTxt dataTxt = JsonConvert.DeserializeObject <DataTxt>(File.ReadAllText(file + IndexFormat));
                    dataTxt.words = CountWords(text);
                    File.WriteAllText(file + IndexFormat, JsonConvert.SerializeObject(dataTxt));
                }
            }
        }