我想知道我是否想要跟踪递归方法中的文件夹数量,我怎么能这样做,这样每次在下面代码的循环中计数器都不会被重置?
// how to keep track of the number of sourceFolder that has been processed?
public void recursiveMethod(SPFolder sourceFolder, SPFolder destinationFolder)
{
int totalNumberOfFiles = sourceFolder.SubFolders.Count;
foreach (SPFolder sourceSubFolder in sourceFolder.SubFolders)
{
if (true)
{
SPFolder destSubFolder = null;
if (true)
{
destSubFolder = doSomething();
}
else
{
destSubFolder = doSomethingElse();
}
}
recursiveMethod(sourceSubFolder, destSubFolder);
}
}
此处的目标是向用户显示“处理文件夹 15 / 100”之类的内容。我无法保留文件夹数而不重置它。假设:文件夹本身没有唯一的ID。
感谢。
答案 0 :(得分:3)
此代码允许您显示到目前为止已处理的文件夹数。然而,它并没有得到文件夹的总量,因为这很棘手:在整个递归完成之前你无法知道:
public void recursiveMethod(SPFolder sourceFolder, SPFolder destinationFolder)
{
int folderIndex = 1;
recursiveMethod(sourceFolder, destinationFolder, ref folderIndex);
}
public void recursiveMethod(SPFolder sourceFolder, SPFolder destinationFolder, ref folderIndex)
{
int totalNumberOfFiles = sourceFolder.SubFolders.Count;
foreach (SPFolder sourceSubFolder in sourceFolder.SubFolders)
{
// Display processed folder
Console.WriteLine("Processing folder " + folderIndex);
if (true)
{
SPFolder destSubFolder = null;
if (true)
{
destSubFolder = doSomething();
}
else
{
destSubFolder = doSomethingElse();
}
}
// Increase processed folder index
folderIndex++;
// Recursive call
recursiveMethod(sourceSubFolder, destSubFolder, ref folderIndex);
}
}
答案 1 :(得分:2)
你能把count变量作为参数传递给方法吗?