• C# 获取指定文件夹中所有的文件(包括子文件夹的文件)


      有个需求中需要播放指定路径的声音,但你必须要有该路径的声音才可以播放,如果没有该文件则播放默认的声音,该方法用于初始化应用的时候获取指定目录的所有文件,便于后来播放声音的时判断路径是否存在。

    using System;
    using TopDAL;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                string res = "";         
           ForeachFile(@"C:UsersaaaDesktopMvcApplication1ConsoleApplicationsound",ref res); Console.Write(res); Console.ReadKey(); }

            /// <summary>
            /// 遍历指定文件夹中的文件包括子文件夹的文件
            /// </summary>
            /// <param name="filePathByForeach">等待遍历的目录(绝对路径)</param>
            /// <param name="result">遍历之后的结果</param>
            /// <returns></returns>

           public static void ForeachFile(string filePathByForeach,ref string result)

            {
                DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
                DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
                FileInfo[] file=  theFolder.GetFiles();//获取所在目录的文件
         
                foreach (FileInfo fileItem in file) //遍历文件
                {
                    result+="dirName:"+fileItem.DirectoryName+"    fileName:"+fileItem.Name + "
    ";
                }
                //遍历文件夹
                foreach (DirectoryInfo NextFolder in dirInfo)
                {
                    ForeachFile(NextFolder.FullName, ref  result );
                }
            }
                 
        }
    }

     

  • 相关阅读:
    HDU 4864 Task(贪心值得学习)
    使程序在Linux下后台运行
    KMP算法
    优先队列的使用
    POJ 2761 Feed the dogs(树状数组求区间第K大)
    HDU 3584 Cube (三维树状数组)
    HDU 1892 See you~ (二维树状数组)
    POJ 1195 Mobile phones(二维树状数组)
    HDU 1166 敌兵布阵 (树状数组和线段树解法)
    POj 1703 Find them, Catch them(关系并查集)
  • 原文地址:https://www.cnblogs.com/lin494910940/p/12396576.html
Copyright © 2020-2023  润新知