/// <summary>
 /// Gets <see cref="FileData"/> for all the files in a directory that match a
 /// specific filter.
 /// </summary>
 /// <param name="path">The path to search.</param>
 /// <param name="searchPattern">The search string to match against files in the path.</param>
 /// <returns>An object that implements <see cref="IEnumerable{FileData}"/> and
 /// allows you to enumerate the files in the given directory.</returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="path"/> is a null reference (Nothing in VB)
 /// </exception>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="filter"/> is a null reference (Nothing in VB)
 /// </exception>
 public static IEnumerable <FileData> EnumerateFiles(
     string path,
     string searchPattern)
 {
     return(FastDirectoryEnumerator.EnumerateFiles(
                path,
                searchPattern,
                SearchOption.TopDirectoryOnly));
 }
        /// <summary>
        /// Gets <see cref="FileData"/> for all the files in a directory that match a
        /// specific filter.
        /// </summary>
        /// <param name="path">The path to search.</param>
        /// <param name="searchPattern">The search string to match against files in the path.</param>
        /// <returns>An object that implements <see cref="IEnumerable{FileData}"/> and
        /// allows you to enumerate the files in the given directory.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="path"/> is a null reference (Nothing in VB)
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="filter"/> is a null reference (Nothing in VB)
        /// </exception>
        public static FileData[] GetFiles(
            string path,
            string searchPattern,
            SearchOption searchOption)
        {
            IEnumerable <FileData> e = FastDirectoryEnumerator.EnumerateFiles(
                path,
                searchPattern,
                searchOption);
            List <FileData> list = new List <FileData>(e);

            FileData[] retval = new FileData[list.Count];
            list.CopyTo(retval);

            return(retval);
        }
 /// <summary>
 /// Gets <see cref="FileData"/> for all the files in a directory.
 /// </summary>
 /// <param name="path">The path to search.</param>
 /// <returns>An object that implements <see cref="IEnumerable{FileData}"/> and
 /// allows you to enumerate the files in the given directory.</returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="path"/> is a null reference (Nothing in VB)
 /// </exception>
 public static IEnumerable <FileData> EnumerateFiles(string path)
 {
     return(FastDirectoryEnumerator.EnumerateFiles(path, "*"));
 }
Esempio n. 4
0
        public static string CopyFilesRecursively2(
            string sourcePath,
            string targetPath,
            Logger logger = null)
        {
            GC.Collect();

            if (targetPath[targetPath.Length - 1] != Path.DirectorySeparatorChar)
            {
                targetPath += Path.DirectorySeparatorChar;
            }

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            var copyFile  = string.Empty;
            var copyCount = 0;

            try
            {
                //Now Create all of the directories
                foreach (string dirPath in Directory.GetDirectories(
                             path: sourcePath,
                             searchPattern: "*",
                             searchOption: SearchOption.AllDirectories))
                {
                    Directory.CreateDirectory(
                        dirPath.Replace(
                            sourcePath,
                            targetPath));
                }

                //Copy all the files & Replaces any files with the same name
                FileData[] files = FastDirectoryEnumerator.GetFiles(
                    path: sourcePath,
                    searchPattern: "*.*",
                    searchOption: SearchOption.AllDirectories);
                LogIt($"{files.Length} Files to copy from {sourcePath} to {targetPath}", logger);
                for (int i = 0; i < files.Length; i++)
                {
                    copyFile = files[i].Path;
                    File.Copy(
                        copyFile,
                        copyFile.Replace(
                            sourcePath,
                            targetPath),
                        overwrite: true);
                    copyCount++;
                }
                var returnMessage = $"Copied {copyCount} files from {sourcePath} to {targetPath}";
                //LogIt(returnMessage, logger)
                return(returnMessage);
            }
            catch (IOException ioex)
            {
                if (ioex.Message.Contains("The process cannot access the file"))
                {
                    LogIt($"{copyFile} File is in use", logger);
                    return("IO Error");
                }
                else if (ioex.Message.Contains("Insufficient system resources"))
                {
                    LogIt($"too many files? memory={GC.GetTotalMemory(true)}", logger);
                    Process proc = Process.GetCurrentProcess();
                    LogIt($"  Physical Memory usage={proc.WorkingSet64}", logger);
                    LogIt($"  Paged sys Memory size={proc.PagedSystemMemorySize64}", logger);
                    LogIt($"  Paged Memory size    ={proc.PagedMemorySize64}", logger);
                    return("Too many");
                }
                else
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                LogIt($"Exception after {copyCount} copies : {ex.Message}", logger);
                throw;
            }
        }