zoukankan      html  css  js  c++  java
  • 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 );
                }
            }
                 
        }
    }

     

  • 相关阅读:
    人脸识别最新开发经验demo分享
    虹软人脸识别SDK的接入方法
    基于虹软sdk,java实现人脸识别(demo)
    【C#】 基于ArcFace 2.0—视频人脸识别Demo
    基于免费人脸识别的闸机开发及源码分享
    运算符及其应用
    vim 命令
    children lastchild parentNode parentElement
    同步对象锁有效作用域
    在eclipse导入Java 的jar包的方法 JDBC【图文说明】
  • 原文地址:https://www.cnblogs.com/lin494910940/p/12396576.html
Copyright © 2011-2022 走看看