排除包含子目录的目录? [英] Exclude directories that contain sub directories?

查看:135
本文介绍了排除包含子目录的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

var directories =  Directory.GetDirectories(
        environmentSettings.SourcePath, 
        "*",
        SearchOption.AllDirectories)
    .Where(dir => !environmentSettings.FolderExclusions
                                      .Contains(Path.GetFileName(dir)));

我想创建在目标路径的目录,除了那些出现在排除列表中。这工作,但前提是该目录是直属的根源,不包含子目录。

I want to create a directory in the target path except for ones that show up in the exclusion list. This works, but only if the directory is directly under the root and does not contain sub-directories.

作为一个例子,如果排除列表中包含自定义称为目录的根目录是 C:\应用程序,这将排除 C:\ APP \自定义,而不是在目标路径创建它,但是一旦遇到类似 C:\应用程序\自定义\子,它最终会在目标路径上创建此。

As an example, if the exclusion list contains a directory called Custom and the root directory is C:\App, it will exclude C:\App\Custom and not create it in the target path, but as soon as it encounters something like C:\App\Custom\Sub, it will end up creating this on the target path.

无法得到它的工作的文件:

Can't get it to work for File:

//所有文件复制

var files = Directory.GetFiles(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
      .Select(file => new FileInfo(file))
      .Where(file => !environmentSettings.FolderExclusions
                                         .Contains(file.Name) && 
                     !environmentSettings.FolderExclusions
                                         .Contains(file.Directory.Name));

上面的问题是,我不知道该怎么告诉内嵌在文件中,例如,如果是在 C:\ APP \自定义\的Thumbs.db ,它工作正常,我想,但如果是在 C:\ APP \自定义\ SUB1 \ SUB 2 \的Thumbs.db ,它仍然会复制文件,自定义目录,这是我不想要的东西。基本上,我需要得到正确低于目录c:\ APP ,如果是自定义,然后我排除的文件

The problem above, is that I don't know how to tell how nested the file is, for example, if it is under c:\App\Custom\thumbs.db, it works fine, I think, but if it is under c:\App\Custom\sub1\sub2\thumbs.db, it still copies the file and the Custom directory which is what I don't want. I basically need to get the directory right below c:\App and if that is Custom, then I exclude the file.

var files = Directory.GetFiles(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
            .Select(file => new FileInfo(file))
            .Where(file => !environmentSettings.FolderExclusions.Contains(file.Directory.Name) && (file.Directory.Parent == null || !environmentSettings.FolderExclusions.Contains(file.Directory.Parent.Name)));

当我通过我的所有文件运行下面的code循环,并把它们在目标目录中,我碰到一个目录未发现异常:

When I run the following code to loop through all my files and put them in a target directory, I run into a Directory not found exception:

//Copy all the files
foreach (var file in files)
{
File.Copy(file.Name, file.FullName.Replace(environmentSettings.SourcePath, environmentSettings.TargetPath));
}

问题是,源文件可能像\网络\ APP1 \ one.mp3,我的目标目录可能是C:\ programdata \ MyApp的。它说,它不能找到 one.mp3 复制从源文件时。不知道如何处理这个问题。

The problem is that the source file maybe something like \network\app1\one.mp3 and my target directory might be c:\programdata\myapp. It is saying it can't find one.mp3 when copying the files from the source. Not sure how to handle this.

推荐答案

试试这个,如果此去只是为了父文件夹和第一子文件夹:

Try this if this goes just for the parent folder and the first sub folder:

var directories =  Directory.GetDirectories(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
    .Select(dir => new DirectoryInfo(dir))
    .Where(dir=>!excludes.Contains(dir.Name) && (dir.Parent == null || !excludes.Contains(dir.Parent.Name)));

要启用此处理文件,请尝试以下

To enable this with files, try the following

var files = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories)
    .Select(file => new FileInfo(file))
    .Where(file=>!excludes.Contains(file.Directory.Name) && (file.Directory.Parent == null || !excludes.Contains(file.Directory.Parent.Name)));

这篇关于排除包含子目录的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆