請看下面的代碼片段。我在「this.directories.Add(new directory(s));」中得到nullreferenceexception。遞歸似乎一直工作到「解開」爲止,此時「新目錄」似乎是空的。我不確定它爲什麼這樣做,我想也許有特殊的規則,因爲遞歸是在構造函數中。請幫忙。構造函數遞歸
namespace AnalyzeDir
{
class directory
{
public string[] files;
public ArrayList directories;
public string mypath;
public string myname;
public directory(string mp)
{
mypath = mp;
myname = mypath.Substring(mypath.LastIndexOf("\\"));
files = Directory.GetFiles(mypath);
fillDirectoriesRescursive();
}
public void fillDirectoriesRescursive()
{
string[] dirpaths = Directory.GetDirectories(mypath);
if (dirpaths != null && (dirpaths.Length > 0))
{
foreach(string s in dirpaths)
{
this.directories.Add(new directory(s));
}
}
}
這與問題無關,但供將來參考:通常使用大寫,即'Directory'和'FillDirectoriesRecursive'開始.NET類和方法名稱。 – 2011-12-20 00:15:04