loner_li 机试题 通过递归方式访问目录树和通过堆栈方式访问 目录树

 1. 通过递归方式访问目录树

   class RecursiveAccessDirectory

    {
        //声明并实例化一个字符串集合
        static System.Collections.Specialized.StringCollection log
            = new System.Collections.Specialized.StringCollection();
        static void Main()
        {
        /*
         循环访问指定目录下的文件夹
         */
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            DirectoryInfo rootDir = new DirectoryInfo(@"C:\");
            WalkDirectoryTree(rootDir);
            stopwatch.Stop();
            TimeSpan timespan = stopwatch.Elapsed;
            Console.WriteLine(timespan.Minutes+"分"+timespan.Seconds+"秒");

            Console.ReadLine();
        }
        static void WalkDirectoryTree(DirectoryInfo root)
        {
            FileInfo[] files = null;
            DirectoryInfo[] subDirs = null;
            try
            {
                files = root.GetFiles();
            }
            catch (System.UnauthorizedAccessException e)
            {

                log.Add(e.Message);
            }
            catch(System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
            if (files!=null)
            {
                foreach (FileInfo fi in files)
                {
                    Console.WriteLine("{0}:{1}{2}",fi.FullName,fi.Length,fi.CreationTime);
                }
                subDirs = root.GetDirectories();
                foreach (DirectoryInfo dirInfo in subDirs)
                {
                    WalkDirectoryTree(dirInfo);
                }
            }
        }

    }


2.通过堆栈方式访问 目录树

 static void Main(string[] args)
        {
            
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
           TraverseTree(@"D:\兴华\");
            
            stopwatch.Stop();
            TimeSpan timespan = stopwatch.Elapsed;
            Console.WriteLine("执行时间为:{0}分{1}秒{2}毫秒", timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
            Console.Read();
           
        }
        public static void TraverseTree(string root)
        {
            Stack<string> dirs = new Stack<string>(20);
            if (!Directory.Exists(root))
            {
                throw new ArgumentException();
            }
            dirs.Push(root);
            while (dirs.Count > 0)
            {
                string currDir = dirs.Pop();
                string[] subDirs;
                try
                {
                    subDirs = Directory.GetDirectories(currDir);

                }
                catch (System.UnauthorizedAccessException e)
                {

                    Console.WriteLine(e.Message);
                    continue;
                }
                catch (System.IO.DirectoryNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
                string[] files = null;
                try
                {
                    files = Directory.GetFiles(currDir);
                }
                catch (System.UnauthorizedAccessException e)
                {

                    Console.WriteLine(e.Message);
                    continue;
                }
                catch (System.IO.DirectoryNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }

                foreach (string file in files)
                {
                    if (file != null)
                    {
                        FileInfo fi = new FileInfo(file);
                        var str = string.Format("{0}: {1} {2}", fi.FullName, fi.Length, fi.CreationTime);
                        Console.WriteLine("{0}: {1} {2}", fi.FullName, fi.Length, fi.CreationTime);
                        var logfile = @"D:\兴华\liang.txt";
                        var aFile = new FileStream(logfile, FileMode.OpenOrCreate,FileAccess.Write);
                        var sw = new StreamWriter(aFile, Encoding.Default);
                        aFile.Position = aFile.Length;
                        Encoding encoder = Encoding.Default;
                        byte[] bytes = encoder.GetBytes(str + "\n");
                        aFile.Write(bytes, 0, bytes.Length);
                        sw.Close();
                    }

                }
                foreach (string str in subDirs)
                {
                    dirs.Push(str);
                }
            }
        }


你可能感兴趣的:(loner_li 机试题 通过递归方式访问目录树和通过堆栈方式访问 目录树)