c# 获取路径文件中的多种文件格式的文件

本文介绍如何使用C#代码获取指定目录下多种文件格式的完整路径,包括.mdb和.gdb等,通过DirectoryInfo和Directory类实现,适用于文件管理和数据处理场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在利用c#开发过程中遇到想要获取某个文件路径的问题,如想获取一个文件夹的所有.mdb的文件则,可以使用如下方法:

 1 public static List<string> GetAllMdbFiles(string mdbPath)
 2         {
 3             if(!Directory.Exists(mdbPath))
 4                 return null;
 5             List<string> mdbList = new List<string>();
 6             string[] list=Directory.GetFiles(mdbPath,"*.mdb",SearchOption.AllDirectories);
 7             if (list.Length == 0 || list == null)
 8                 return null;
 9             for (int i = 0; i < list.Length; i++)
10             {
11                 mdbList.Add(list[i]);
12             }
13             return mdbList;
14         }

如果当前文件夹内只有。mdb类型的文件也就满足要求了,可实际上文件夹中存储的文件格式可能不一定是.mdb,也可能是.gdb格式的,或者是二者共存的情况,那么我们要取得所有指定格式的文件名该如何去做呢?下面引用网上的一段代码:http://topic.youkuaiyun.com/u/20090317/10/00646e69-5d3d-43d8-a033-a9c75b069100.html

 private ArrayList GetFiles(string sPath, string[] sPt) //sPath是路径,sPt是文件后缀的数组
        {
            DirectoryInfo dir = new DirectoryInfo(sPath);
            ArrayList Files = new ArrayList();
            FileInfo[] tmp;

            foreach (string s in sPt)
            {
                tmp = dir.GetFiles(s);
                foreach (FileInfo fi in tmp)
                {
                    Files.Add(fi);
                }
            }
            return Files;
        }

受此启发简单改善下开始的源代码

 

 1   public static List<string> GetAllMdbFiles(string mdbPath,string[]spt)
 2           {
 3               if(!Directory.Exists(mdbPath))
 4                   return null;
 5               List<string> mdbList = new List<string>();
 6                foreach(string s in spt)
 7                {
 8                      string[] list=Directory.GetFiles(mdbPath,s,SearchOption.AllDirectories);
 9                      if (list.Length == 0 || list == null)
10                         continue;
11                      for (int i = 0; i < list.Length; i++)
12                      {
13                        mdbList.Add(list[i]);
14                      }
15                }16                 return mdbList;
17              }

 

转载于:https://www.cnblogs.com/daidaigua/archive/2012/04/23/2466629.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值