跳出递归(较快方式跳出)

递归方式查找文件, 如果找到,较快方式跳出递归。

   

Private bool beFind=false;

private bool GetFileFromFileLocation(string currentFolder)

{

try

{

if (Directory.Exists(currentFolder))

{

//

foreach (string folder in Directory.GetDirectories(currentFolder))

{

if (beFind)//先判断befind 如果true, 跳出foreach.

break;

string[] files=Directory.GetFiles(folder);//判断有文件就 使 befind =true; 然后跳出

if (files.Length != 0)

{

beFind = true;

break;

}

   

string[] folders = Directory.GetDirectories(folder); //查找文件夹

if(folders.Length!=0&&!beFind)//如果有文件夹 并且beFind =false;

{

beFind = GetFileFromFileLocation(folder);// 递归 flase 赋予 beFind, 直到 beFind =ture

}

}

}

else

{

GetConfigurationInfo();// 重新获取this.ueFileLocation路径

GetFileFromFileLocation(this.ueFileLocation);

}

}

catch (IOException ex)

{

string message = string.Format(ex.Message);

MessageBox.Show(this, message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

//throw new DirectoryNotFoundException(currentFolder+"not find");

}

return beFind;

}

posted on 2008-09-25 18:30  fanet  阅读(1333)  评论(0编辑  收藏  举报