7

ジャンクション ポイントの罠に陥ることなく、C# を使用してフォルダー構造をトラバースするにはどうすればよいでしょうか。

4

2 に答える 2

9

知らない人のために: ジャンクション ポイントは、Linux のフォルダーのシンボリック リンクと同様に動作します。言及されているトラップは、次のように再帰的なフォルダー構造を設定すると発生します。

given folder /a/b
let /a/b/c point to /a
then
/a/b/c/b/c/b becomes valid folder locations.

このような戦略を提案します。Windowsでは、パス文字列の最大長に制限されているため、再帰的なソリューションはおそらくスタックを吹き飛ばしません。

private void FindFilesRec(
    string newRootFolder,
    Predicate<FileInfo> fileMustBeProcessedP,
    Action<FileInfo> processFile)
{
    var rootDir = new DirectoryInfo(newRootFolder);
    foreach (var file in from f in rootDir.GetFiles()
                         where fileMustBeProcessedP(f)
                         select f)
    {
        processFile(file);
    }

    foreach (var dir in from d in rootDir.GetDirectories()
                        where (d.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint
                        select d)
    {
        FindFilesRec(
            dir.FullName,
            fileMustBeProcessedP,
            processFile);
    }
}
于 2008-11-19T09:37:00.903 に答える
-2

次のコードを使用できます。

private void processing(string directory)
        {
            cmbFilesTypesSelectedIndex = cmbFilesTypes.SelectedIndex;
            CheckForProjectFile(directory);
            DirectoryInfo dInfo = new DirectoryInfo(directory);
            DirectoryInfo[] dirs = dInfo.GetDirectories() ;
            foreach (DirectoryInfo subDir in dirs)
            {
                CheckForProjectFile(subDir.FullName);
                processing(subDir.FullName);
            }
        }

        private void CheckForProjectFile(string directory)
        {
            Boolean flag = false; 
            DirectoryInfo dirInfo = new DirectoryInfo(directory);
            FileInfo[] files = dirInfo.GetFiles();
            //You can also traverse in files also
            foreach (FileInfo subfile in files)
            {
                //Do you want

            }
        }
于 2008-11-19T09:45:28.283 に答える