Hello,
I'm creating a toolbox of useful functions, and I've added a function
that recursively drills down through a file system searching for files.
I've created a number of variations of this function, and I imagine
I'll continue to have use for it.
What I would like to do is create a version that will take a method as
a parameter and invoke it as it traverses my file system path. Here is
some pseudo-code that will hopefully illustrate what I'm trying to do:
public class Toolbox{
public void (string startDirectory, Method myMethod) fileDriller{
foreach (string f in Directory.GetFi les(startDirect ory){
myMethod(f);
}
foreach (string d in Directory.GetDi rectories(start Directory){
fileDriller(d, myMethod);
}
}
}
So let's say I wanted to determine the length of each filename in a
directory of interest and every subdirectory:
public void lengthOfFileNam e(string fileName){
Console.write(f ileName.Length. toString());
}
static void Main(){
Toolbox tb = new Toolbox();
tb.fileDriller( "C:\\somedi r", lengthOfFileNam e);
}
I tried passing a MethodInfo object, but I'm running into issues
because, I believe, it's an abstract class. If anyone has any idea how
I would accomplish this, please let me know!
Regards,
Chris
I'm creating a toolbox of useful functions, and I've added a function
that recursively drills down through a file system searching for files.
I've created a number of variations of this function, and I imagine
I'll continue to have use for it.
What I would like to do is create a version that will take a method as
a parameter and invoke it as it traverses my file system path. Here is
some pseudo-code that will hopefully illustrate what I'm trying to do:
public class Toolbox{
public void (string startDirectory, Method myMethod) fileDriller{
foreach (string f in Directory.GetFi les(startDirect ory){
myMethod(f);
}
foreach (string d in Directory.GetDi rectories(start Directory){
fileDriller(d, myMethod);
}
}
}
So let's say I wanted to determine the length of each filename in a
directory of interest and every subdirectory:
public void lengthOfFileNam e(string fileName){
Console.write(f ileName.Length. toString());
}
static void Main(){
Toolbox tb = new Toolbox();
tb.fileDriller( "C:\\somedi r", lengthOfFileNam e);
}
I tried passing a MethodInfo object, but I'm running into issues
because, I believe, it's an abstract class. If anyone has any idea how
I would accomplish this, please let me know!
Regards,
Chris
Comment