コード例 #1
0
ファイル: FileDeleter.cs プロジェクト: jespersv/LogArchiver
        public bool TryDeleteFiles(IEnumerable <string> files, bool deleteFiles)
        {
            var filesListed   = files.ToList();
            var filteredFiled = filesListed.Where(f => !_fileProvider.IsFileLocked(f)).ToList();

            if (filteredFiled.Count != filesListed.Count)
            {
                _log.Info($"Some files where locked. Amount: {filesListed.Count - filteredFiled.Count}");
            }

            foreach (var file in filteredFiled)
            {
                try
                {
                    _fileProvider.DeleteFile(file, deleteFiles);
                }
                catch (Exception e)
                {
                    _log.Error(e);
                    retryCount++;
                    _log.Error($"Exception occured while deleting filePath: {file}. Retry {retryCount} out of {retryMaxCount}");
                    if (retryCount <= retryMaxCount)
                    {
                        _threadProvider.Sleep(waitBeforeRetryMs);
                        return(TryDeleteFiles(filteredFiled, deleteFiles));
                    }
                }
            }

            var filesDeleted = filteredFiled.All(f => !_fileProvider.Exists(f));

            _log.Info(filesDeleted ? $"Files were successfully deleted" : $"Some files were not deleted");

            return(filesDeleted);
        }
コード例 #2
0
        public async Task DeleteFile(Guid fileId)
        {
            var file = await context.Files.FindAsync(fileId);

            if (file is null)
            {
                return;
            }

            var fileInfo = await fileProvider.GetFile(file.RelativeFolderPath, file.FileName, file.Extension);

            await fileProvider.DeleteFile(fileInfo);
        }
コード例 #3
0
        public void Provider_Can_Delete_File()
        {
            // Arrange
            string testFile   = TestHelper.GetFullPath(TestFile);
            string importFile = TestHelper.GetFullPath(ImportFile);

            File.WriteAllText(importFile, Properties.Resources.file);

            // Act
            _provider.DeleteFile(importFile);

            // Assert
            Assert.IsTrue(!File.Exists(importFile));
        }
コード例 #4
0
        public void CanWriteReadDelteFile()
        {
            var tempDir  = Path.GetTempPath();
            var tempFile = Path.GetRandomFileName();

            var fullName = Path.Combine(tempDir, tempFile);

            FileProvider.CreateFileAsync(fullName, "Jake").GetAwaiter().GetResult();
            var file = FileProvider.GetFileInfo(fullName);

            Assert.IsTrue(file.Exists);
            Assert.AreEqual(fullName, file.Path);
            Assert.AreEqual("Jake", file.ReadAllTextAsync().GetAwaiter().GetResult());

            FileProvider.DeleteFile(fullName);

            file = FileProvider.GetFileInfo(fullName);

            Assert.IsFalse(file.Exists);
        }
コード例 #5
0
        public int Cut(string startLocation, string endLocation)
        {
            int totalFiles  = 0;
            int copiedFiles = 0;

            if (String.IsNullOrEmpty(startLocation))
            {
                throw new ArgumentException("Start Location is Required!");
            }

            if (String.IsNullOrEmpty(endLocation))
            {
                throw new ArgumentException("End Location is Required!");
            }

            Console.WriteLine($"+++ Cutting Files From {startLocation} to {endLocation} +++");

            // now that we have both the start and end locations - we want to attempt to read both locations
            bool startLocationExists = _fp.FindFolderLocation(startLocation);
            bool endLocationExists   = _fp.FindFolderLocation(endLocation);

            if (!startLocationExists)
            {
                throw new FileNotFoundException("Error accessing start location!");
            }

            if (!endLocationExists)
            {
                throw new FileNotFoundException("Error accessing end location!");
            }

            // now that we have determined that both locations exist - enumerate the folders in both - so we can compare the difference and create the necessary folders
            IEnumerable <string> startLocationFolders = _fp.FindAllFolders(startLocation);
            IEnumerable <string> endLocationFolders   = _fp.FindAllFolders(endLocation);

            // we have all folders - compare those in the end location to those in the start location
            // and create the missing folders
            IEnumerable <string> missingFolders = startLocationFolders.Except(endLocationFolders);

            if (missingFolders.Count() > 0)
            {
                Console.WriteLine("The following folders are in StartLocation but not EndLocation and will be created: ");
                foreach (string s in missingFolders)
                {
                    Console.WriteLine(s);
                }

                // now create the missing folders
                _fp.CreateFolders(missingFolders, endLocation);
            }



            // now we need to go through all folders and find the files in each and copy them over - then delete the original file
            // TODO: - Add a argument wether we want to override existing files

            foreach (var folder in startLocationFolders)
            {
                List <string>        files        = _fp.FindAllFiles(startLocation + folder);
                List <string>        endFiles     = _fp.FindAllFiles(endLocation + folder);
                IEnumerable <string> missingFiles = files.Except(endFiles);
                // list all the files - just for debugging
                totalFiles += missingFiles.Count();

                foreach (string file in missingFiles)
                {
                    PrintCurrentCopyingFile(file);
                    // now copy the missing files over
                    int result = _fp.CopyFile(startLocation + folder + file, endLocation + folder + file);
                    copiedFiles += result;
                }

                // now delete the original files - regardless of if they were missing or not
                // TODO - Add an argument to delete only the copied files

                foreach (string file in files)
                {
                    _fp.DeleteFile(startLocation + folder + file);
                }
            }
            Console.Write('\r');
            Console.WriteLine($"+++ {copiedFiles}/{totalFiles} Files Moved Successfully +++");
            return(1);
        }
コード例 #6
0
 public void DeleteSetupSettings(string setupSettingsFilePath = null)
 {
     setupSettingsFilePath ??= SetupSettingsConst.SetupConfigurationPath;
     _fileProvider.DeleteFile(setupSettingsFilePath);
 }