问题
I have the following 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.
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:
//Copy all the files
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));
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)));
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));
}
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.
回答1:
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)));
来源:https://stackoverflow.com/questions/12646507/exclude-directories-that-contain-sub-directories