Ejemplo n.º 1
0
        public List <FilePathDate> GetFilesFromFolder(string sourcePath, DateTime lastDateWrited)
        {
            try
            {
                string[] patterns        = { "*.LTD*", "*.LOG" };
                var      currentLTDFiles = MySearch(sourcePath, patterns);
                foreach (string currentFilePath in currentLTDFiles)
                {
                    DateTime lastWriteTime = File.GetLastWriteTime(currentFilePath);
                    if (lastWriteTime >= lastDateWrited)
                    {
                        AllLTDFilesPathsInFolders.Add(new FilePathDate()
                        {
                            Path = currentFilePath,
                            LastChangeDateTime = lastWriteTime
                        });
                    }
                }

                MessageShowMethod.ShowMethod("Обнаружено " + AllLTDFilesPathsInFolders.Count + " новых файлов");
            }
            catch (Exception ex)
            {
                MessageShowMethod.ShowMethod(ex.Message);
                MessageShowMethod.ShowMethod("Ошибка в методе GetFilesFromFolder");
            }
            return(AllLTDFilesPathsInFolders);
        }
Ejemplo n.º 2
0
        private bool MoveFile(string currentFilePath)
        {
            try
            {
                if (currentFilePath.StartsWith(SourcePath))
                {
                    string sourceFileName   = currentFilePath;
                    string destFileName     = currentFilePath.Replace(SourcePath, DestPath);
                    var    newDirectoryPath = Path.GetDirectoryName(destFileName);
                    Directory.CreateDirectory(newDirectoryPath);
                    File.Copy(sourceFileName, destFileName, true);

                    MessageShowMethod.ShowMethod("Скопирован файл " + currentFilePath);
                    return(true);
                }
                else
                {
                    MessageShowMethod.ShowMethod("Путь источнику был изменён, не удалось скопировать файл: " + currentFilePath);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                MessageShowMethod.ShowMethod(ex.Message);
                MessageShowMethod.ShowMethod("Ошибка в работе метода MoveFile при копировании файла " + currentFilePath);
                return(false);
            }
        }
Ejemplo n.º 3
0
        public void MoveFiles()
        {
            try
            {
                while (NewFilesPaths.Count > 0)
                {
                    string newFilePath = NewFilesPaths[0].Path;

                    if (MoveFile(newFilePath))
                    {
                        NewDateTimeCopy = NewFilesPaths[0].LastChangeDateTime;
                        NewFilesPaths.RemoveAt(0);
                        if (Interval > 0)
                        {
                            Thread.Sleep(Interval * 1000);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageShowMethod.ShowMethod(ex.Message);
                MessageShowMethod.ShowMethod("Прерывание метода MoveFiles");
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Принимает дату, записывает её в файл
 /// </summary>
 /// <param name="startDateCopy"></param>
 public void SetDateToFile(DateTime startDateCopy)
 {
     try
     {
         if (startDateCopy != null)
         {
             FileInfo   lastDateTimeFile = new FileInfo(FilePath);
             FileStream fs;
             var        serializer = new XmlSerializer(typeof(DateTime));
             if (lastDateTimeFile.Exists)
             {
                 fs = lastDateTimeFile.Open(FileMode.Truncate, FileAccess.Write);
             }
             else
             {
                 fs = lastDateTimeFile.Create();
             }
             lastDateTimeFile.Attributes = FileAttributes.Hidden;
             serializer.Serialize(fs, startDateCopy);
             fs.Close();
         }
     }
     catch (Exception ex)
     {
         MessageShowMethod.ShowMethod(ex.Message);
         MessageShowMethod.ShowMethod("Ошибка при работе метода SetDateToFile");
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Если файл существует, десериализует хранящуюся в файле дату. Иначе возвращает нулевую дату
        /// </summary>
        /// <returns>Возвращает дату</returns>
        public DateTime GetDataFromFile()
        {
            var      serializer = new XmlSerializer(typeof(DateTime));
            DateTime startDate  = new DateTime();

            try
            {
                FileInfo lastDateTimeFile = new FileInfo(FilePath);
                if (File.Exists(FilePath))
                {
                    FileStream fs = lastDateTimeFile.OpenRead();
                    startDate = (DateTime)serializer.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                MessageShowMethod.ShowMethod(ex.Message);
                MessageShowMethod.ShowMethod("Ошибка при работе метода GetDataFromFile");
            }
            return(startDate);
        }
Ejemplo n.º 6
0
        public void ReadSettingsFile()
        {
            IsActual = true;
            try
            {
                if (File.Exists(SettingsFilePath))
                {
                    using (StreamReader sr = new StreamReader(SettingsFilePath))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            var linePart = line.Split('|');
                            switch (linePart.Length)
                            {
                            case 1:
                                Int32.TryParse(line, out _IntervalAllCile);
                                MessageShowMethod.ShowMethod("Принята настройка временного интервала: " + _IntervalAllCile + " секунд.");
                                break;

                            case 3:
                                if (Directory.Exists(linePart[0].Trim()))
                                {
                                    Settings.Add(new Settings(
                                                     linePart[(int)SettingsLineParts.SourcePathPart].Trim(),
                                                     linePart[(int)SettingsLineParts.BufferPathPart].Trim(),
                                                     linePart[(int)SettingsLineParts.ServiceNamePart].Trim()));
                                    MessageShowMethod.ShowMethod("Принята настройка для " + linePart[(int)SettingsLineParts.ServiceNamePart].Trim());
                                }
                                else
                                {
                                    MessageShowMethod.ShowMethod("Не удалось принять настройку для " + linePart[(int)SettingsLineParts.ServiceNamePart].Trim());
                                }
                                break;

                            default:
                                MessageShowMethod.ShowMethod("Не верно задана настройка ");
                                break;
                            }
                        }
                    }
                    if (Settings.Count > 0)
                    {
                        MessageShowMethod.ShowMethod("Настройки приняты успешно");
                    }
                    else
                    {
                        IsActual = false;
                    }
                }
                else
                {
                    MessageShowMethod.ShowMethod("Файл c настройками отсутствует");
                    IsActual = false;
                }
            }
            catch (Exception ex)
            {
                MessageShowMethod.ShowMethod(ex.Message);
                MessageShowMethod.ShowMethod("Ошибка при работе метода ReadPathsFromFile");
                IsActual = false;
            }
        }