【问题标题】:c# copying files from source folder to target folderc#将文件从源文件夹复制到目标文件夹
【发布时间】:2013-08-02 07:02:44
【问题描述】:

Source 和 Target 具有相同的子目录,如下所示:

c:\fs\source\a\
c:\fs\源\b\

c:\fs\target\a\
c:\fs\目标\b\

如果不存在文件,我正在努力将文件从源复制到目标。 C# 中比较源文件夹与目标文件夹的最佳方法是什么 - 检查目标文件是否不退出,将文件从特定源(c:\fs\source\a\config.xml 和 app.config)复制到特定目标(c:\fs\目标\a\)。如果存在目标文件,请忽略它。 C#怎么写?

非常感谢您的代码示例。谢谢!

    public void TargetFileCreate()
    {
        foreach (var folder in SourceFolders)
        {
            string[] _sourceFileEntries = Directory.GetFiles(folder);

            foreach (var fileName in _sourceFileEntries)
            {    //dont know how to implement here:
                 //how to compare source file to target file to check if files exist or not
                 //c:\fs\source\A\config.xml compares to c:\fs\target\A\ (no files) that should be pasted
                 //c:\fs\source\B\config.xml compares to c:\fs\target\B\config.xml that is already existed - no paste
            }
        }
    }

【问题讨论】:

  • 我尝试在源文件夹和目标文件夹之间的两个循环(foreach)中进行搜索,您可以在其中比较特定文件夹并检查该文件夹是否没有文件,将文件从源复制到该文件夹​​。我的代码看起来很奇怪。我希望看到更好的编码方式......

标签: c# directory copy-paste directoryinfo system.io.fileinfo


【解决方案1】:
foreach (var file in Directory.GetFiles(source))
{
    File.Copy(file, Path.Combine(target, Path.GetFileName(file)), false);
}

【讨论】:

    【解决方案2】:

    您可以通过这种方式检查每个文件是否存在:

    string curFile = @"c:\temp\test.txt";
    Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
    

    把它放在你的循环中。然后将这些文件复制到那里。

    MSDN 代码:

    // Simple synchronous file copy operations with no user interface. 
    // To run this sample, first create the following directories and files: 
    // C:\Users\Public\TestFolder 
    // C:\Users\Public\TestFolder\test.txt 
    // C:\Users\Public\TestFolder\SubDir\test.txt 
    public class SimpleFileCopy
    {
        static void Main()
        {
            string fileName = "test.txt";
            string sourcePath = @"C:\Users\Public\TestFolder";
            string targetPath =  @"C:\Users\Public\TestFolder\SubDir";
    
            // Use Path class to manipulate file and directory paths. 
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);
    
            // To copy a folder's contents to a new location: 
            // Create a new target folder, if necessary. 
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
    
            // To copy a file to another location and  
            // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);
    
            // To copy all the files in one directory to another directory. 
            // Get the files in the source folder. (To recursively iterate through 
            // all subfolders under the current directory, see 
            // "How to: Iterate Through a Directory Tree.")
            // Note: Check for target path was performed previously 
            //       in this code example. 
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);
    
                // Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }
    
            // Keep console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    

    【讨论】:

      【解决方案3】:
      foreach (var file in Directory.GetFiles(source))
      {
         var targetFile = System.IO.Path.Combine(target, System.IO.Path.GetFileName(file));
         if(!File.Exists(targetFile))
         {
             File.Copy(file, targetFile)
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-03-31
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多