/// <summary>
        /// Creates the new directory at the provided path
        /// </summary>
        /// <param name="path">Path string</param>
        private void CreateDirectoryInternal(string path)
        {
            var directory = _directoryInfoFactory.FromDirectoryName(path);

            if (directory.Exists == false)
            {
                directory.Create();
            }
        }
        public IEnumerable <DisplayFileInfo> MatchFileSystemInfo(string search, string incompleteName, bool isRecursive)
        {
            // search folder and add results
            var directoryInfo   = _directoryInfoFactory.FromDirectoryName(search);
            var fileSystemInfos = directoryInfo.EnumerateFileSystemInfos(incompleteName, new EnumerationOptions()
            {
                MatchType                = MatchType.Win32,
                RecurseSubdirectories    = isRecursive,
                IgnoreInaccessible       = true,
                ReturnSpecialDirectories = false,
                AttributesToSkip         = FileAttributes.Hidden,
                MatchCasing              = MatchCasing.PlatformDefault,
            });

            return(fileSystemInfos
                   .Select(CreateDisplayFileInfo));
        }
Exemple #3
0
        /// <summary>
        /// Handles the cleanup process
        /// </summary>
        /// <param name="command">The command</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns></returns>
        public Task <CleanupDownloadFolderCommandResult> Handle(
            CleanupDownloadFolderCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            var commandResult = new CleanupDownloadFolderCommandResult();

            string downloadFolder = _integrationConfiguration.Value.DownloadFolder;

            var downloadFolderInfo = _directoryInfoFactory.FromDirectoryName(downloadFolder);

            int filesDeleted    = 0;
            int filesNotDeleted = 0;

            if (downloadFolderInfo.Exists)
            {
                var files = downloadFolderInfo.EnumerateFiles();

                foreach (var file in files)
                {
                    try
                    {
                        file.Delete();

                        filesDeleted++;
                    }
                    catch (Exception e)
                        when(
                            e is IOException ||
                            e is SecurityException ||
                            e is UnauthorizedAccessException)
                        {
                            filesNotDeleted++;
                        }
                }
            }

            commandResult.Result          = CleanupDownloadFolderCommandResultKind.Success;
            commandResult.FilesDeleted    = filesDeleted;
            commandResult.FilesNotDeleted = filesNotDeleted;

            return(Task.FromResult(commandResult));
        }
        public MainWindowViewModel(IDirectoryInfoFactory directoryInfoFactory, IFileInfoFactory fileInfoFactory)
        {
            SyncFolders = new ObservableCollection <SyncFolderPairViewModel>();

            var foldersFile = fileInfoFactory.FromFileName(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "LocalSyncFolders.txt"));

            using var reader = foldersFile.OpenText();
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                var parts = line.Split('|');
                if (parts.Length != 2)
                {
                    throw new Exception("Each line in the settings file must be a pair of folders separated by `|`");
                }

                SyncFolders.Add(new SyncFolderPairViewModel(directoryInfoFactory.FromDirectoryName(parts[0].Trim()), directoryInfoFactory.FromDirectoryName(parts[1].Trim())));
            }
        }
Exemple #5
0
        private IEnumerable <IFileInfo> GetFiles(CleanupFolderConfigurationFolder folder, DateTime now)
        {
            IEnumerable <IFileInfo> files;

            var directory = _directoryInfoFactory.FromDirectoryName(folder.Folder);

            if (directory.Exists)
            {
                var minDateTime = now.AddDays(folder.MaxAgeInDays.Value * -1);

                var extensions = folder.Extensions
                                 .Where(x => string.IsNullOrEmpty(x) == false)
                                 .Select(x => x[0] == '.' ? x : string.Concat(".", x))
                                 .ToHashSet(StringComparer.OrdinalIgnoreCase);

                if (extensions.Count > 0)
                {
                    files = directory.EnumerateFiles()
                            .Where(x => string.IsNullOrEmpty(x.Extension) == false)
                            .Where(x => extensions.Contains(x.Extension))
                            .Where(x => x.Exists)
                            .Where(x => x.CreationTimeUtc <= minDateTime)
                            .Where(x => x.LastWriteTimeUtc <= minDateTime);
                    // No ToList()
                }
                else
                {
                    files = Enumerable.Empty <IFileInfo>();
                }
            }
            else
            {
                files = Enumerable.Empty <IFileInfo>();
            }

            return(files);
        }