新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

ASP.NET获取文件夹内所有文件及文件夹列表

 public void listFiles(string dir, int level) 

创新互联自2013年创立以来,先为仓山等服务建站,仓山等地企业,进行企业商务咨询服务。为仓山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

        {  
            try 
            { 
                //获取文件列表 
                string[] files = Directory.GetFiles(dir); 
 
                String preStr = ""; 
                for (int i = 0; i < level; i++) 
                {
                    preStr += "   "; 
                } 
 
                foreach (string f in files) 
                { 
                    if (f.LastIndexOf("\\") == -1) 
                    { 
                        Response.Write(preStr + f.Substring(f.LastIndexOf("/") + 1)+"
"); 
                    } 
                    else 
                    {
                        Response.Write(preStr + f.Substring(f.LastIndexOf("\\") + 1) + "
"); 
                    } 
                } 
 
                //获取目录列表 
                string[] dirs = Directory.GetDirectories(dir); 
                foreach (string d in dirs) 
                { 
                    if (d.LastIndexOf("\\") == -1) 
                    {
                        Response.Write(preStr + d.Substring(d.LastIndexOf("/") + 1) + "
"); 
                    } 
                    else 
                    {
                        Response.Write(preStr + d.Substring(d.LastIndexOf("\\") + 1) + "
"); 
                    } 
                    if (Directory.Exists(d)) 
                    { 
                         listFiles(d, level + 1); 
                    } 
                } 
            } 
            catch (Exception ex) 
            { 
                Response.Write(ex.Message); 
            } 
        } 
 
===========================================================
 
调用:
 
 string testDir = @"E:\EveryDayWorks\2013-1\1-18";
 listFiles(testDir, 0); 

分享标题:ASP.NET获取文件夹内所有文件及文件夹列表
网页链接:http://www.wjwzjz.com/article/gpdsoj.html
在线咨询
服务热线
服务热线:028-86922220
TOP