亲宝软件园·资讯

展开

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

人气:0
想了解asp.net遍历目录文件夹和子目录所有文件的相关内容吗,在本文为您仔细讲解asp.net 遍历目录的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:asp.net,遍历,目录文件夹,子目录,下面大家一起来学习吧。
复制代码 代码如下:

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);
            }
        }
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论