受欢迎的博客标签

How to access each file in each nested subdirectory and copy, delete, and move files (C# Programming Guide)

Published

 

F:\mongodbbackup\20211102\var\www\nopcommerce\wwwiaspnetcorecom\src\Presentation\Nop.Web\wwwroot\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages\uploadimages

.Net 6.x

ConsoleApp1

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Program.cs

// See https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree
// See https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/file-system/how-to-copy-delete-and-move-files-and-folders
Console.WriteLine("Hello, World!");

 System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();

// Start with drives if you have to search the entire computer.
string[] drives = System.Environment.GetLogicalDrives();

foreach (string dr in drives)
{
    System.IO.DriveInfo di = new System.IO.DriveInfo(dr);

    // Here we skip the drive if it is not ready to be read. This
    // is not necessarily the appropriate action in all scenarios.
   
    if (!di.IsReady)
    {
        Console.WriteLine("The drive {0} could not be read", di.Name);
        continue;
    }
    else
    {
        Console.WriteLine("The drive {0} could  be read", di.Name);
    }
    //System.IO.DirectoryInfo rootDir = di.RootDirectory;

    System.IO.DirectoryInfo rootDir = new DirectoryInfo(@"F:\mongodbbackup\20211102\var\www\nopcommerce\wwwiaspnetcorecom\src\Presentation\Nop.Web\wwwroot\uploadimages");

  

    WalkDirectoryTree(rootDir);

    // Write out all the files that could not be processed.
    Console.WriteLine("Files with restricted access:");
    foreach (string s in log)
    {
        Console.WriteLine(s);
    }
    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key");
    Console.ReadKey();

    static void WalkDirectoryTree(System.IO.DirectoryInfo root)
    {
        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo[] subDirs = null;

        // First, process all the files directly under this folder
        try
        {
            files = root.GetFiles("*.*");
        }
        // This is thrown if even one of the files requires permissions greater
        // than the application provides.
        catch (UnauthorizedAccessException e)
        {
            // This code just writes out the message and continues to recurse.
            // You may decide to do something different here. For example, you
            // can try to elevate your privileges and access the file again.
          //  log.Add(e.Message);
        }

        catch (System.IO.DirectoryNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }

        //https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/file-system/how-to-copy-delete-and-move-files-and-folders
        string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath = @"f:\SubDir";

        if (files != null)
        {
            foreach (System.IO.FileInfo fi in files)
            {
                // In this example, we only access the existing FileInfo object. If we
                // want to open, delete or modify the file, then
                // a try-catch block is required here to handle the case
                // where the file has been deleted since the call to TraverseTree().
                Console.WriteLine(fi.FullName);

                // Use Path class to manipulate file and directory paths.
              //  string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                string destFile = System.IO.Path.Combine(targetPath, fi.Name);

                //overwrite the destination file if it already exists.
                System.IO.File.Copy(fi.FullName, destFile, true);

                Console.WriteLine(fi.FullName + "to" + destFile);

            }

            // Now find all the subdirectories under this directory.
            subDirs = root.GetDirectories();

            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                // Resursive call for each subdirectory.
                WalkDirectoryTree(dirInfo);
            }
        }
    }
}