【问题标题】:Need to copy files from one location to another if path matches neglecting the root folder(s)如果路径匹配忽略根文件夹,则需要将文件从一个位置复制到另一个位置
【发布时间】:2017-04-18 09:36:35
【问题描述】:

如果路径匹配,我需要将文件从一个位置复制到另一个位置。在这种情况下(附图片),我有一个包含 Text.txt 的文件夹 C:\OldFiles\New Folder\,我还有另一个包含 Text.txt 的文件夹 D:\NewFiles\New Folder\。请注意,根文件夹和子文件夹不同,但文件名和文件夹名完全相同。

开发一个 Windows 窗体 C# 工具,该工具指向包含新文件的路径,该新文件应替换不同路径中的旧文件。请帮忙?点击here查看我的场景。

【问题讨论】:

  • 我的问题是如何执行此操作。?
  • 忽略根文件夹是什么意思?
  • 只检查文件名及其父文件夹而不检查 C:\ 或 D:\ 或其子文件夹的条件。 "C:\OldFiles\"New Folder\ 和 "D:\NewFiles\"New Folder\ 例如。 @BchirMedAmine

标签: c# winforms


【解决方案1】:
 string fileName = "test.txt";
            string sourcePath = @"C:\OldFiles\New Folder\";
            string targetPath =  @"D:\NewFiles\New Folder\ ";

            // 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
            {
                //"Source path does not exist!
            }

【讨论】:

    【解决方案2】:
                string sourcePath = @"C:\OldFiles\NewFolder";
                string targetPath = @"D:\NewFiles\NewFolder ";
    
    
                var a = sourcePath.Split('\\');
                var b = targetPath.Split('\\');
                string a1 = a[a.Length - 2]; //this will return OldFiles
                string a2 = a[a.Length-1]; //this will return NewFolder
                string b1 = b[b.Length - 2]; // this will return  NewFiles
                string b2 = b[b.Length-1]; // this will return NewFolder
    
                //you get the idea now use what ever you want with it in you if statement
    

    【讨论】:

      【解决方案3】:
      // pass true to replace existing if exists in destination 
      File.Copy("source path here", "destination path here", true);
      

      【讨论】:

        猜你喜欢
        • 2021-12-18
        • 1970-01-01
        • 2011-05-03
        • 2013-05-02
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        相关资源
        最近更新 更多