c# 生成文件目录树

 class Program
    {
        //遍历目录名含有M00到M11的目录
        //生成文件目录树(去除文件名中含有scc\Designer\designer\resx的文件)
        //生成的文件保存在D:\\ak.txt
        public static string[] folds = new string[] { "M00", "M01", "M02", "M03", "M04", "M05", "M06", "M07", "M08", "M09", "M10", "M11" };
        public static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"D:\xxxx\xxxx\xxxx\xxxx\UI");
            FindFile(di);
            Console.WriteLine("生成完成");
            Console.ReadKey();
        }
        static void FindFile(DirectoryInfo di)
        {

     
            DirectoryInfo[] dis = di.GetDirectories();
            for (int j = 0; j < dis.Length; j++)
            {
                //如果目录中含有M00-M11 则进行读取文件名的操作
                var fullName = dis[j].FullName;
                var numForLine = fullName.LastIndexOf("\\")+1;
                var tureName = fullName.Substring(numForLine);
                var cutFullName = tureName.Substring(0,3);
                bool exists = folds.Contains(cutFullName);
                if (exists)
                {
                    Console.WriteLine("目录:" + dis[j].FullName);
                    sb.Append(tureName + "\r\n");
                    //获取文件名
                    FileInfo[] fis = dis[j].GetFiles();
                    for (int i = 0; i < fis.Length; i++)
                    {
                        var filefullName = fis[i].FullName;
                        var filenumForLine = filefullName.LastIndexOf("\\") + 1;
                        var filetureName = filefullName.Substring(filenumForLine);
                        //scc
                        //Designer
                        //designer
                        //resx
                        if (!filetureName.Contains("Designer")&&!filetureName.Contains("designer") && !filetureName.Contains("scc") && !filetureName.Contains("resx"))
                        {
                            Console.WriteLine("文件:" + filetureName);
                            sb.Append("    |--" + filetureName + "\r\n");
                        }
                       
                    }
                }
             
                FindFile(dis[j]);
            }
            Write(sb.ToString());
        }
        public static void Write(string txt)
        {
            FileStream fs = new FileStream("D:\\ak.txt", FileMode.Create);
            //获得字节数组
            byte[] data = System.Text.Encoding.Default.GetBytes(txt);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();
        }
    }

 

posted @ 2019-05-24 16:18  幸福安康  阅读(1266)  评论(0编辑  收藏  举报