Visual Studio 2005 Code Snippets is wrong (Search a Directory for Files Recursively)

Z

Zytan

You can download them here:
http://msdn2.microsoft.com/en-us/vstudio/aa718338.aspx

This snippet seems wrong:
Visual C# 2005 Code Snippets -> filesystem -> Search a Directory for
Files Recursively

I don't believe DirSearch will return any results for files in the
root directory originally passed to it. Am I right? Here's the code:

System.Collections.ArrayList files = new
System.Collections.ArrayList();
DirSearch(@"C:\", ref files);

//Method to Search Directory for specified file Type.Paste it outside
the function
public void DirSearch(string sDir, ref System.Collections.ArrayList
files)
{
try
{
foreach (string d in System.IO.Directory.GetDirectories(sDir))
{
foreach (string f in System.IO.Directory.GetFiles(d,
"*.txt"))
{
files.Add(f);
}
DirSearch(d, ref files);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}

Zytan
 
C

Chris Dunaway

You can download them here:http://msdn2.microsoft.com/en-us/vstudio/aa718338.aspx

This snippet seems wrong:
Visual C# 2005 Code Snippets -> filesystem -> Search a Directory for
Files Recursively

I don't believe DirSearch will return any results for files in the
root directory originally passed to it. Am I right? Here's the code:

System.Collections.ArrayList files = new
System.Collections.ArrayList();
DirSearch(@"C:\", ref files);

//Method to Search Directory for specified file Type.Paste it outside
the function
public void DirSearch(string sDir, ref System.Collections.ArrayList
files)
{
try
{
foreach (string d in System.IO.Directory.GetDirectories(sDir))
{
foreach (string f in System.IO.Directory.GetFiles(d,
"*.txt"))
{
files.Add(f);
}
DirSearch(d, ref files);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}

}

Zytan

I believe you are right. You can edit the snippet to correct if you
need to.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top