代码改变世界

递归遍历目录

2008-06-03 17:14  老羽  阅读(250)  评论(0编辑  收藏  举报
代码很简单:
private void RecursiveDirectory(DirectoryInfo folder)
        
{
            FileInfo[] files 
= folder.GetFiles();  
            
foreach(FileInfo file in files)
            
{
               
//do something
            }


            DirectoryInfo[] subFolders 
= folder.GetDirectories( );
            
if(subFolders == null || subFolders.Length == 0)
            
{
                
return;
            }

            
foreach(DirectoryInfo subFolder in subFolders)
            
{
                
if(_isCancel)
                
{
                    
return;
                }

                RecursiveDirectory(subFolder);
            }

        }
调用:
DirectoryInfo root = new DirectoryInfo(path);
RecursiveDirectory(root);

递归的算法很简单,如果能掌握其精神,任何类似的代码都是手到擒拿。