Here is the code I had used. It doesn’t quit from the loop. Please let me know where I made the things wrong.
private bool LocalWindowsTest(string path)
{
bool Flag = false;
DirectoryInfo dir = new DirectoryInfo(path);
foreach (DirectoryInfo d in dir.GetDirectories())
{
foreach (string fileExt in new string[] { "*.csproj", "*.vbproj" })
{
foreach (FileInfo file in d.GetFiles(fileExt))
{
Flag = true;
break;
}
if (Flag) break;
}
if (!Flag)
LocalWindowsTest(d.FullName);
else
return Flag;
}
return Flag;
}

How can I quit the execution from the recursive function?
Jan Kučera
I think what you want is this just after the first for loop:
if( Flag || LocalWindowsTest( d.FullName)) return true;
Actually, a much cleaner way to write the whole thing:
Gokhan_Varol
Hi Figo Fei / Axe,
Thank you so much for your prompt reply. This is the code exactly what I want.
lemmy101
Logically, it should be
if (!Flag)
Flag = LocalWindowsTest(d.FullName);
else
return Flag;
But it wont fire errors.
I guess that you have passed bad param, which caused errors.
kWazar
Thanks. Actually it should quit the recursion once it meet certain condition else it should continue with the recursion.
If you see the code in my first update, I am checking for .csproj / .vbproj and set a bool flag and it should quit the recursion at that point. But it does not work in my case. It keeps continuing with the recursion.
goku_simon
Hi, Prabu
I've tried your code, it seems to work well. So can you show how you use this method, and what error is exposed during the runtime
sureshsundar007
Hi, Prabu
I read the code again, and found that you'd set an exit for a recursive function, just as Axe decribed.
In your code: if (!Flag)
LocalWindowsTest(d.FullName);----this may make the loop continue
else
return Flag;
And the code may never touch the leaves, which must return sth to exit the recursive.
Consider processing the case that the input param is null or some empty string.
Further more, you'd judge if the directory exist at first.
So your code gonna be :
public static bool LocalWindowsTest(string path)< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
{
if ((path == null) || (path.Equals(string.Empty)))
return false;
DirectoryInfo dir = new DirectoryInfo(path);
if (!dir.Exists)
return false;
foreach (DirectoryInfo d in dir.GetDirectories())
{
foreach (string fileExt in new string[] { "*.csproj", "*.vbproj" })
{
foreach (FileInfo file in d.GetFiles(fileExt))
{
return true;
}
}
if (LocalWindowsTest(d.FullName)) return true;
}
return false;
}
Thanks