C# 复制目录中的所有内容并重命名复制的文件

C# 复制目录中的所有内容并重命名复制的文件,c#,directory,subdirectory,C#,Directory,Subdirectory,我尝试将文件夹中的所有内容复制到另一个文件夹中,在本例中是从“sourceFolder”复制到“targetFolder” 假设“sourceFolder”中有两个文件,还有两个子文件夹和一个附加文件: (我试着表现出来) 现在,我正在尝试将所有这些文件和子文件夹从“sourceFolder”复制到“targetFolder”,子文件夹的标题应相同,并且在文件上,我希望在“Filex.txt”前面添加“a”(例如:“aFilex.txt”) 这就是我想要得到的: //targetFolder /

我尝试将文件夹中的所有内容复制到另一个文件夹中,在本例中是从“sourceFolder”复制到“targetFolder”

假设“sourceFolder”中有两个文件,还有两个子文件夹和一个附加文件: (我试着表现出来)

现在,我正在尝试将所有这些文件和子文件夹从“sourceFolder”复制到“targetFolder”,子文件夹的标题应相同,并且在文件上,我希望在“Filex.txt”前面添加“a”(例如:“aFilex.txt”) 这就是我想要得到的:

//targetFolder
//├aFile1.txt
//├aFile2.txt
//├Subfolder1
//| └aFile3.txt
//|
//└Subfolder2
//  └aFile4.txt
当前代码:

string[] sourceDirectoryFiles = Directory.GetFiles(sourceFolderTextbox.Text);
string[] sourceDirectorysubfolders = Directory.GetDirectories(targetFolderTextbox.Text);
string sourcedirectory = sourceFolderTextbox.Text;
string targetdirectory = targetFolderTextbox.Text;

    if (Directory.Exists(sourceDirectorysubfolders[0]))
    {
        foreach (string sourceFilePath in sourceDirectorysubfolders)
        {
                if (!Directory.Exists(sourceFilePath))
                    {
                        Directory.CreateDirectory(sourceFilePath.Replace(sourcedirectory, targetdirectory));
                    }
            }
    }

    foreach (string sourceFilePath in sourceDirectoryFiles )
    {
        string newsourcefilePath = String.Empty;
        string newfilePath = String.Empty;
        string FileName = System.IO.Path.GetFileName(sourceFilePath);
        newsourcefilePath = sourcedirectory + "\\a" + FileName;

        System.IO.File.Copy(sourceFilePath, newfilePath ,true)
    }
我希望,我问清楚:) 否则我会回答你的问题:)


我不擅长英语或编程,因此欢迎提出建设性的批评:)

已修改此解决方案以满足您的需要。希望能有帮助

void Copy(string sourcePath, string targetPath)
{

    foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
        Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));

    string newPath;
    foreach (string srcPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
    {
        newPath = srcPath.Replace(sourcePath, targetPath);
        newPath = newPath.Insert(newPath.LastIndexOf("\\") + 1, "a"); //prefixing 'a'
        newPath = newPath +  ".example";
        File.Copy(srcPath, newPath, true);
    }
}

你的样品不完整。也就是说,没有为SurceFolders或surceFolderFiles定义变量。我希望现在更好:)那么什么不起作用?您可能还希望重命名所有变量以写出
source
,而不是
surce
。目前它只将sourceFolder中的文件复制到targetFolder中,并创建子文件夹。但它不会复制子文件夹中的文件/子文件夹。现在,我会使用前缀a,但如果我想给它第二个扩展名(.txt.example),我会问这个问题,因为我是一个新手,需要学习/理解
void Copy(string sourcePath, string targetPath)
{

    foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
        Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));

    string newPath;
    foreach (string srcPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
    {
        newPath = srcPath.Replace(sourcePath, targetPath);
        newPath = newPath.Insert(newPath.LastIndexOf("\\") + 1, "a"); //prefixing 'a'
        newPath = newPath +  ".example";
        File.Copy(srcPath, newPath, true);
    }
}