using system;
using system.io;
using system.collections;
namespace dsclub
{
/**//// <summary>
/// dirlist 的摘要說明。
/// </summary>
public class dirlist
{
private string strinitfilepath;
private bool bfatchall;
// 構(gòu)造函數(shù)
public dirlist()
{
bfatchall = false;
strinitfilepath = "c://";
}
public dirlist(string strfilepath)
{
bfatchall = false;
strinitfilepath = strfilepath;
}
// 是否遞歸出所有的文件
public bool recursionfiles
{
get
{
return bfatchall;
}
set
{
bfatchall = value;
}
}
// 取得文件的函數(shù)
public arraylist getfiles()
{
return getfiles(strinitfilepath, bfatchall);
}
public static arraylist getfiles(string strpath, bool resultsall)
{
arraylist al = new arraylist();
// 判斷路徑是否存在
if(!directory.exists(strpath))
{
throw(new applicationexception("訪問的路徑" + strpath + "不存在,或者它不是個文件夾。"));
}
string[] temp = directory.getfiles(strpath);
foreach(string afile in temp)
{
al.add(afile);
}
// 如果此目錄下不存在文件,則把文件夾路徑返回,并用///作標(biāo)識
if(temp.length == 0)
{
al.add("http:///" + strpath);
}
if(resultsall)
{
temp = directory.getdirectories(strpath);
foreach(string adir in temp)
{
al.addrange(getfiles(adir, resultsall));
}
}
return al;
}
}
}
其中關(guān)于應(yīng)該有系統(tǒng)文件的檢查,還有用戶不可訪問系統(tǒng)文件夾的判斷,但是這個項(xiàng)目中用不上,又不想用try塊兒影響效率。
還是遞歸的思想!
新聞熱點(diǎn)
疑難解答
圖片精選