Ejemplo n.º 1
0
        /// <summary>
        /// Checks if a file contains only valid characters.
        /// Checks synchronization every line read.
        /// </summary>
        /// <param name="filePath">The path to the file.</param>
        /// <returns>True if the file contains only valid characters else false.</returns>
        public async Task <bool> Validate(string filePath, PauseOrCancelToken syncToken)
        {
            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    while (reader.Peek() >= 0)
                    {
                        await syncToken.PauseOrCancel();

                        string line = reader.ReadLine();
                        foreach (char c in line)
                        {
                            if (!isLegalCharacter(c))
                            {
                                return(false);
                            }
                        }
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Checks if the file is valid.
 /// With synchronization
 /// </summary>
 /// <param name="filePath">Path to the file.</param>
 /// <returns>True if the file is valid, else false.</returns>
 private async Task <bool> isValid(string filePath, PauseOrCancelToken syncToken)
 {
     if (!nameValidator.Validate(filePath))
     {
         return(false);
     }
     return(await contentValidator.Validate(filePath, syncToken));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Searches given directory for all valid files.
        /// </summary>
        /// <param name="root">Directory root.</param>
        /// <param name="progress">Syncronization progress reporter.</param>
        /// <param name="syncToken">Token to cancel or pause the operation.</param>
        public async Task Search(string root, IProgress <FileSearchProgressModel> progress, PauseOrCancelToken syncToken)
        {
            await syncToken.PauseOrCancel();

            if (!Directory.Exists(root))
            {
                throw new ArgumentException($"{root} Doesn't exist.");
            }
            Stack <string> directories = new Stack <string>();

            directories.Push(root);
            while (directories.Count > 0)
            {
                string   currentDirectory = directories.Pop();
                string[] subDirectories;
                string[] files;
                try
                {
                    subDirectories = Directory.GetDirectories(currentDirectory);
                    files          = Directory.GetFiles(currentDirectory);
                }
                catch (UnauthorizedAccessException)
                {
                    continue;
                }
                catch (DirectoryNotFoundException)
                {
                    continue;
                }

                foreach (string file in files)
                {
                    await syncToken.PauseOrCancel();

                    progress.Report(new FileSearchProgressModel(file, false, false));
                    await syncToken.PauseOrCancel();

                    bool valid = await isValid(file, syncToken);

                    progress.Report(new FileSearchProgressModel(file, true, valid));
                }
                foreach (string subDirectory in subDirectories)
                {
                    directories.Push(subDirectory);
                }
            }
        }