簡體   English   中英

獲取文件夾中的所有目錄/文件

[英]Getting all dirs/files in a folder

我將僅具有根文件夾的樹狀視圖,我使用以下遞歸函數,但我認為該函數的返回值存在問題,因此我無法擁有所有文件/文件夾和某些子文件/文件夾缺失:

 public string DirSearch(string sDir)
    {
        string result = "";
        string physicalPath = sDir;
        if (sDir == "")
        {
            sDir = "~";
            physicalPath = HttpContext.Current.Server.MapPath(sDir);
        }
        try
        {
            foreach (string d in Directory.GetDirectories(physicalPath))
            {
                result += d;
                foreach (string f in Directory.GetFiles(d))
                {
                    result += f + ",";
                }
                result += ";";
                DirSearch(d);
            }
        }
        catch (System.Exception excpt)
        {

        }
        return result;
    }

這是怎么了? 函數的返回值有問題嗎?

       result += ";";
        DirSearch(d);

您可以在子文件夾中進行搜索,但不要將其添加到結果中,例如,

       result += ";" + DirSearch(d);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM