野生大熊猫

C# 获取某个目录下文件数
using System; using System.Collections.Generic; using Sys...
扫描右侧二维码阅读全文
07
2018/06

C# 获取某个目录下文件数

QQ截图20180607001855.png

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static long FileOrDircout(string path)
        {
            long count = 0;

            try
            {
                //统计file个数
                var files = Directory.GetFiles(path);
                count += files.LongLength;

                //统计dir个数
                var dirs = Directory.GetDirectories(path);
                foreach (var dir in dirs)
                {
                    count += FileOrDircout(dir);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }





            return count;
        }

        static void Main(string[] args)
        {

            Console.WriteLine(FileOrDircout("D:/"));

            Console.Read();

        }
    }
}
Last modification:June 7th, 2018 at 12:19 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment