Reputation: 43
I am tryting to display the number of tmp files recursively using the below code. But when I call the DirSearch() for recursive purpose; I get an error No overload method can take 1 argument.
namespace TestForm
{
public partial class TEST : Form
{
public TEST()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TEST search = new TEST();
search.DirSearch();
int result = search.DirSearch();
label1.Text = result.ToString();
}
public int DirSearch()
{
int count = 0;
var sDir = Directory.GetFiles(@"C:\", "*.tmp", SearchOption.AllDirectories);
try
{
foreach (string d in sDir)
{
foreach (string f in Directory.GetFiles(d, "*.tmp"))
{
string extension = Path.GetExtension(f);
if (extension != null && (extension.Equals(".tmp")))
{
count++;
return count;
}
}
DirSearch(d);
}
}
catch (UnauthorizedAccessException)
{
}
}
}
}
Upvotes: 1
Views: 411
Reputation: 16966
You should be bit cautious when writing recursive
code.
In your program/example
signature
has to match for DirSearch
.return count;
statement, code returns just after counting one file. You are not counting all of them.Please note, recursion comes with additional performance penality (Might not be for all cases, i'm leaving it to you to explore your case).
Below code will help you to do what you need.
public static int DirSearch(string root)
{
int count = Directory.GetFiles(root, "*.tmp", SearchOption.TopDirectoryOnly).Count();
foreach (string dir in Directory.GetDirectories(root))
{
count += Directory.GetFiles(dir, "*.tmp", SearchOption.TopDirectoryOnly).Count();
count += DirSearch(dir);
}
return count;
}
Hope this helps !!
Upvotes: 0
Reputation: 371
Are you just trying to get the number of *.tmp files on the C:\ drive? If so, this should do the trick:
int fileCount = Directory.GetFiles(@"C:\", "*.tmp", SearchOption.AllDirectories).Length;
Maybe I'm missing something - but I'm not sure why you are trying to do everything else in that method, or what the point of recursion would be here.
Upvotes: 0
Reputation: 1495
I think you are slightly misunderstanding the recursive algorithm you are trying to implement.
For a recursive method to work, it needs to operate on an input and then call itself again (recursively) on derived inputs.
Your DirSearch() method does not take any input, as such it will compute the same thing over and over again. You need to pass in the root path to the method at which the recursion algorithm will start, so the signature should change to:
public int DirSearch(string rootPath)
Then change:
var sDir = Directory.GetFiles(rootPath, "*.tmp", SearchOption.AllDirectories);
Now in the body of the DirSearch method you have to enumerate all directories in the provided path and then call the DirSeach method with the full path to these directories:
DirSearch(d)
Upvotes: 3
Reputation: 12682
Your definition of DirSearch()
does not take any argument, so you cannot call it with a direction.
Maybe you could remove this
var sDir = Directory.GetFiles(@"C:\", "*.tmp", SearchOption.AllDirectories);
from your method and add it before calling DirSearch()
in your event, passing this first sDir
as a parameter, and adding the parameter in the method definition.
Also, you should consider on handling the exception properly
Upvotes: 0