【问题标题】:How to copy images in one folder to another folder using c#? [duplicate]如何使用c#将一个文件夹中的图像复制到另一个文件夹? [复制]
【发布时间】:2012-07-17 06:07:44
【问题描述】:

可能重复:
How to copy a file to another path?

当我的代码开始执行时,我需要将图像从一个文件夹复制到另一个文件夹。每次它开始执行时都应该发生这种情况。

【问题讨论】:

标签: c#


【解决方案1】:
Public void CopyFiles(string sourcePath,string destinationPath)
{
     string[] files = System.IO.Directory.GetFiles(sourcePath);

     foreach(string file in files)
     {
        System.IO.File.Copy(sourcePath,destinationPath);  
     }
}

【讨论】:

    【解决方案2】:

    我的看法是使用以下代码 -

    public void CopyFiles(string sourcePath, string destinationPath)
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);
            Parallel.ForEach(files, file =>
            {
                System.IO.File.Copy(file, System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(file)));
    
            });
        }
    

    【讨论】:

    • PLINQ 太棒了! (仅在 .NET 4 及更高版本中)。它通过对源集合进行分区来工作,并根据系统环境将工作安排在多个线程上。系统上的处理器越多,并行方法的运行速度就越快。但是请参阅数据和任务并行性中的潜在陷阱here is the link
    【解决方案3】:

    您可以使用File.Copy 方法将文件从一个位置复制到另一个位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 2014-09-17
      相关资源
      最近更新 更多