菜鸟的问题
好记性不如烂笔头~。~

1.获取指定文件夹下所有文件的路劲信息

using Microsoft.IdentityModel.Protocols;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Async
{
    public class Program
    {

        public static void Main(string[] args)
        {
            #region 获取文件夹下所有文件的完整路径
            string strPaths = ConfigurationManager.AppSettings["dirPath"];
            List<FileInfo> lst = new List<FileInfo>();
            List<FileInfo> lstfiles = GetInfo(strPaths, lst);
            DirectoryInfo info = new DirectoryInfo(strPaths);
            foreach (FileInfo item in lstfiles)
            {
                var index = lstfiles.IndexOf(item);
                Console.WriteLine($"循环第{index}次  -  取到对应的文件信息:{item}");
            }
            Console.ReadKey();
            #endregion
        }

        #region 获取文件夹下所有文件的完整路径
        /// <summary>
        /// 获取文件夹下所有文件的完整路径
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="lst">返回参数</param>
        /// <returns></returns>
        private static List<FileInfo> GetInfo(string path, List<FileInfo> lst)
        {
            try
            {
                //string[] dirInfo = Directory.GetDirectories(path);
                string[] fileList = Directory.GetFileSystemEntries(path);
                foreach (string item in fileList)
                {
                    FileInfo fi = new FileInfo(item);
                    if (fi.Extension != " ") // 获取路径下的所有文件路径
                    {
                        lst.Add(fi);
                    }

                }
                return lst;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        #endregion
    }
}
View Code

2.获取指定文件夹下所有文件的路劲信息(包括子文件夹)

using Microsoft.IdentityModel.Protocols;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Async
{
    public class Program
    {

        public static void Main(string[] args)
        {
            #region 获取文件夹下所有文件的完整路径(包括子文件夹)
            string strPaths = ConfigurationManager.AppSettings["dirPath"];
            List<string> lstfiles = GetInfoPath(strPaths);
            DirectoryInfo info = new DirectoryInfo(strPaths);
            foreach (var item in lstfiles)
            {
                var index = lstfiles.IndexOf(item);
                Console.WriteLine($"循环第{index}次  -  取到对应的文件信息:{item}");
            }
            Console.ReadKey();
            #endregion
        }

        #region 获取文件夹下的所有文件的完整路径(包括子文件夹)

        static List<string> list = new List<string>();//定义list变量,存放获取到的路径
        /// <summary>
        /// 获取文件夹下的所有文件的完整路径(包括子文件夹)
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static List<string> GetInfoPath(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] fil = dir.GetFiles();
            DirectoryInfo[] dii = dir.GetDirectories();
            foreach (FileInfo f in fil)
            {
                list.Add(f.FullName);//添加文件的路径到列表
            }

            //获取子文件夹内的文件列表,递归遍历
            foreach (DirectoryInfo d in dii)
            {
                GetInfoPath(d.FullName);
                list.Add(d.FullName);//添加文件夹的路径到列表
            }
            return list;
        }
        #endregion
    }
}
View Code

3.App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <appSettings>
    <add key="dirPath" value="E:\1\"/>
    <add key="Img" value="D:\Img\"/>
  </appSettings>
</configuration>
View Code

 

posted on 2019-10-30 18:11  ArSang-Blog  阅读(3017)  评论(0编辑  收藏  举报