Пример #1
0
        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            if (!target.FullName.Contains("Z:"))
            {
                if (!Directory.Exists(target.FullName))
                {
                    Directory.CreateDirectory(target.FullName);
                }

                // ToCopy each file into the new directory.
                foreach (FileInfo fi in source.GetFiles())
                {
                    fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
                    ConsoleWriteLine.WriteInConsole(null, null, "Done", string.Format(@"Copying {0}\{1}", target.FullName, fi.Name), ConsoleColor.White);
                }

                // ToCopy each subdirectory using recursion.
                foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
                {
                    DirectoryInfo nextTargetSubDir =
                        target.CreateSubdirectory(diSourceSubDir.Name);
                    CopyAll(diSourceSubDir, nextTargetSubDir);
                }
            }
        }
Пример #2
0
        public static void DeleteAll(DirectoryInfo source)
        {
            if (!source.FullName.Contains("Z:"))
            {
                FileInfo[] GetFiles = default;

                try
                {
                    GetFiles = source.GetFiles();
                }
                catch (UnauthorizedAccessException ex)
                {
                }

                if (GetFiles is null ? false : GetFiles.Count() > 0) // Delete each file into the new directory.
                {
                    foreach (FileInfo fi in GetFiles)
                    {
                        ConsoleWriteLine.WriteInConsole(null, null, "Done", string.Format(@"Delete {0}\{1}", source.FullName, fi.Name), ConsoleColor.White);

                        try
                        {
                            fi.Delete();
                        }
                        catch { }
                    }
                }

                foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) // Delete each subdirectory using recursion.
                {
                    DeleteAll(diSourceSubDir);

                    try
                    {
                        diSourceSubDir.Delete();
                    }
                    catch
                    {
                        DeleteAll(diSourceSubDir);
                    }
                }
            }
        }