当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net遍历目录文件夹和子目录所有文件

asp.net遍历目录文件夹和子目录所有文件

2018年04月22日  | 移动技术网IT编程  | 我要评论

侧梦美人,t8002,飞龙在天4

复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.io;
using system.threading;

namespace copefile
{
    class program
    {
        static void main(string[] args)
        {
            string testdir = "e:/xunlei/";
            listfiles(testdir,0);
            console.readkey();
        }

        public static void listfiles(string dir, int level)
        {
           //阿会楠练习作品,程序多有参考
            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)
                    {
                        console.writeline(prestr + f.substring(f.lastindexof("/") + 1));
                    }
                    else
                    {
                        console.writeline(prestr + f.substring(f.lastindexof("\\") + 1));
                    }

                }

                //获取目录列表
                string[] dirs = directory.getdirectories(dir);
                foreach (string d in dirs)
                {
                    if (d.lastindexof("\\") == -1)
                    {
                        console.writeline(prestr + d.substring(d.lastindexof("/") + 1));
                    }
                    else
                    {
                        console.writeline(prestr + d.substring(d.lastindexof("\\") + 1));
                    }
                    if (directory.exists(d))
                    {
                        listfiles(d, level + 1);
                    }
                }

            }
            catch (exception ex)
            {
                console.writeline(ex.message);
            }
        }
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网