使用Directory类,检查某些文件类型的目录

时间:2012-11-19 01:40:03

标签: c# directory

我一直在使用以下行搜索特定文件类型的文件夹结构,只是复制这些文件类型并保持其原始文件夹结构。它工作得很好。 我可以对我的方法进行任何修改,只复制包含过滤文件类型的目录吗?

*编辑:我可以让用户使用名为txtFilter的文本框选择一组特定文件(例如* .dwg或* .pdf)。

private void button1_Click(object sender, EventArgs e)
{
    string sourceFolder = txtSource.Text;
    string destinationFolder = txtDestination.Text;
    CopyFolderContents(sourceFolder, destinationFolder);
}

// Copies the contents of a folder, including subfolders to an other folder, overwriting existing files
public void CopyFolderContents(string sourceFolder, string destinationFolder)
        {
            string filter = txtFilter.Text;

            if (Directory.Exists(sourceFolder))
            {
                // Copy folder structure
                foreach (string sourceSubFolder in Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories))
                {
                    Directory.CreateDirectory(sourceSubFolder.Replace(sourceFolder, destinationFolder));
                }

                // Copy files
                foreach (string sourceFile in Directory.GetFiles(sourceFolder, filter, SearchOption.AllDirectories))
                {
                    string destinationFile = sourceFile.Replace(sourceFolder, destinationFolder);
                    File.Copy(sourceFile, destinationFile, true);
                }
            }
        }

3 个答案:

答案 0 :(得分:1)

你的主循环中是这样的吗?

if (Directory.EnumerateFiles(sourceSubFolder, "*.pdf").Any())
       Directory.CreateDirectory(sourceSubFolder.Replace(sourceFolder, destinationFolder));

或多种文件类型:

if (Directory.EnumerateFiles(sourceSubFolder).Where(x => x.ToLower.EndsWith(".pdf") || x.ToLower.EndsWith(".dwg")).Any())
       Directory.CreateDirectory(sourceSubFolder.Replace(sourceFolder, destinationFolder));

答案 1 :(得分:0)

您可以简单地将两个操作连接到一个循环中,并在O(n)中完成算法。

foreach(string sourceFile in Directory.GetFiles(sourceFolder, filter, SearchOption.AllDirectories))
{
    Directory.CreateDirectory(Path.GetDirectoryName(sourceFile.Replace(sourceFolder,destinationFolder)));
    File.Copy(sourceFile,sourceFile.Replace(sourceFolder,destinationFolder),true);
}

答案 2 :(得分:0)

您可以从找到的文件中获取不同的目录,然后在复制文件之前迭代它们并创建目录。

        if (Directory.Exists(sourceFolder))
        {
            var files = Directory.GetFiles(sourceFolder, filter, SearchOption.AllDirectories);

            foreach(string directory in files.Select(f => Path.GetDirectoryName(f)).Distinct())
            {
                string destinationDirectory = directory.Replace(sourceFolder, destinationFolder);

                if (!Directory.Exists(destinationDirectory))
                {
                    Directory.CreateDirectory(destinationDirectory);                        
                }
            }

            foreach (string sourceFile in files)
            {
                string destinationFile = sourceFile.Replace(sourceFolder, destinationFolder);
                File.Copy(sourceFile, destinationFile, true);
            }
        }