我有这段代码:
private void SearchForDoc()
{
try
{
outputtext = @"c:\temp\outputtxt";
outputphotos = @"c:\temp\outputphotos";
temptxt = @"c:\temp\txtfiles";
tempphotos = @"c:\temp\photosfiles";
if (!Directory.Exists(temptxt))
{
Directory.CreateDirectory(temptxt);
}
if (!Directory.Exists(tempphotos))
{
Directory.CreateDirectory(tempphotos);
}
if (!Directory.Exists(outputtext))
{
Directory.CreateDirectory(outputtext);
}
if (!Directory.Exists(outputphotos))
{
Directory.CreateDirectory(outputphotos);
}
t = Environment.GetEnvironmentVariable(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
ApplyAllFiles(t,ProcessFile(t);
for (int i = 0; i < textfiles.Length; i++)
{
FileInfo fi = new FileInfo((textfiles[i]));
DirectoryInfo d = new DirectoryInfo(temptxt);
long dirSize = DirSize(d);
if ((dirSize + fi.Length) <= 8388608)
fi.CopyTo(temptxt + "\\" + fi.Name, true);
else
break;
}
然后在此之后我有两种方法:
static void ProcessFile(string path) {/* ... */}
static void ApplyAllFiles(string folder, Action<string> fileAction)
{
foreach (string file in Directory.GetFiles(folder))
{
fileAction(file);
}
foreach (string subDir in Directory.GetDirectories(folder))
{
try
{
ApplyAllFiles(subDir, fileAction);
}
catch
{
// swallow, log, whatever
}
}
}
在我的方法中使用这两个方法应该从文档目录及其所有子目录中获取所有文本文件。
在我的方法中,我做了:
ApplyAllFiles(t,ProcessFile(t);
但这是使用它的错误方法。我该如何使用这些方法?
答案 0 :(得分:1)
由于ProcessFile
方法已经与Action<string>
具有相同的签名,因此您只需指定方法名称:
ApplyAllFiles(t, ProcessFile);