0

我正在编写一个脚本来从我的构建中生成一些人工制品,所以我想先清理不需要的文件。我正在使用CleanDirectory(dirPath, predicate).

我发现找出文件的目录非常困难。如果我使用GetDirectoryName()它似乎只是让我得到直接的父级,而不是完整的目录路径。

Func<IFileSystemInfo, bool> predicate =
        fileSystemInfo => {

            // Dont filter out any directories
            if (fileSystemInfo is IDirectory)
                return false;

           var path = fileSystemInfo.Path.FullPath;

           var directory = ((DirectoryPath)path).GetDirectoryName();
           ...
}

显然,我可以使用 .NET FrameworkSystem.IO类轻松地做到这一点,但随后我得到带有斜线方向错误的字符串,并且事情不能与使用 POSIX 路径的 Cake 顺利互操作。

4

1 回答 1

0

好的,我已经制定了解决方案。关键IFileSystemInfo是尝试将其Path转换为各种派生类型/接口,然后提供您可能正在寻找的功能。例子:

 Func<IFileSystemInfo, bool> predicate =
        fileSystemInfo => {

            // Dont filter out any directories
            if (fileSystemInfo is IDirectory)
                return false;

            // We can try and cast Path as an FilePath as know it's not a directory
            var file = (FilePath) fileSystemInfo.Path;

            if (file.FullPath.EndsWith("Help.xml", StringComparison.OrdinalIgnoreCase))
                        return false;

            // GetDirectory() returns a Path of type DirectoryPath
            var directory = file.GetDirectory().FullPath;
            ...
    }
于 2017-05-24T01:41:35.170 回答