如何获取某路径下的文件

Hinagi 2009-12-09 01:48:53
应该很简单了,不过我没学过C#,完全不知道改怎么写

就一个函数,传两个参数,一个路径,一个文件后缀名, 返回一个数组/集合/vector/list? 什么都行,找出来就可以了
...全文
137 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
卧_槽 2009-12-09
  • 打赏
  • 举报
回复
getfiles()
数据之巅 2009-12-09
  • 打赏
  • 举报
回复
匹配模式写成这个"*.jpg",顺便贴一个搜索文件夹下及其子文件夹的代码:

/// <summary>
/// 寻找指定文件夹及其子文件夹中的所有指定类型的文件名称
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="lastName">后缀名</param>
/// <returns></returns>
public static string[] GetAllFiles(string filePath ,string lastName)
{
DirectoryInfo sourceFilecInfo = new DirectoryInfo (filePath ) ;
//记录当前文件夹中的子文件夹
DirectoryInfo[] allDirectory = sourceFilecInfo .GetDirectories () ;
//然后对每个依次进行判断其后缀名
string[] res = Directory.GetFiles(filePath,"*."+lastName );

//递归调用该方法来去子文件中的文件进行搜索
for (int i = 0 ; i <allDirectory .Length ; i ++ )
{
string[] tempRes = GetAllFiles (allDirectory [i].FullName ,lastName );
//合并结果 res 与 tempRes
string[] combination = new string[res.Length +tempRes.Length ] ;
int count = 0 ;
foreach (string s in res )
{
combination [count ++]= s ;
}
foreach (string s in tempRes )
{
combination [count ++] = s ;
}
res = combination ;
}
return res ;
}

调用:
string[] s = GetAllFiles (@"D:\Backup\我的文档\","txt");
记住,调用的时候输入的地址后面一定要加一个 "\",或者自己在程序里面改,默认加上也可以
数据之巅 2009-12-09
  • 打赏
  • 举报
回复
匹配模式写成这个"*.jpg",顺便贴一个搜索文件夹下及其子文件夹的代码:

/// <summary>
/// 寻找指定文件夹及其子文件夹中的所有指定类型的文件名称
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="lastName">后缀名</param>
/// <returns></returns>
public static string[] GetAllFiles(string filePath ,string lastName)
{
DirectoryInfo sourceFilecInfo = new DirectoryInfo (filePath ) ;
//记录当前目录下的所有文件,不包括子文件夹中的文件
FileInfo[] allFiles = sourceFilecInfo .GetFiles () ;
//记录当前文件夹中的子文件夹
DirectoryInfo[] allDirectory = sourceFilecInfo .GetDirectories () ;
//然后对每个依次进行判断其后缀名
string[] res = Directory.GetFiles(filePath,"*."+lastName );

//递归调用该方法来去子文件中的文件进行搜索
for (int i = 0 ; i <allDirectory .Length ; i ++ )
{
string[] tempRes = GetAllFiles (allDirectory [i].FullName ,lastName );
//合并结果 res 与 tempRes
string[] combination = new string[res.Length +tempRes.Length ] ;
int count = 0 ;
foreach (string s in res )
{
combination [count ++]= s ;
}
foreach (string s in tempRes )
{
combination [count ++] = s ;
}
res = combination ;
}
return res ;
}

调用:
string[] s = GetAllFiles (@"D:\Backup\我的文档\","txt");
记住,调用的时候输入的地址后面一定要加一个 "\",或者自己在程序里面改,默认加上也可以
数据之巅 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 kenshintang1215 的回复:]
string[] fl = Directory.GetFiles("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\示例图片", ".jpg");

这样写不对么?  为什么取出来fl为空...
[/Quote]
这样写吧:
string[] res = Directory.GetFiles(filePath,"*."+lastName );
string[] res = Directory.GetFiles(C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\示例图片", "*.jpg");
shine333 2009-12-09
  • 打赏
  • 举报
回复
*.jpg
Hinagi 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 asxinyu_usst 的回复:]
引用 5 楼 kenshintang1215 的回复:
引用 1 楼 asxinyu_usst 的回复:
是不是获取指定路径下 同一类型的所有文件的完整地址啊。。?


恩,对

那还要考虑 子文件啊 需要递归寻找。。。
[/Quote]

不用考虑那个了
string[] fl = Directory.GetFiles("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\示例图片", ".jpg");

这样写不对么? 为什么取出来fl为空...
数据之巅 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kenshintang1215 的回复:]
引用 1 楼 asxinyu_usst 的回复:
是不是获取指定路径下 同一类型的所有文件的完整地址啊。。?


恩,对
[/Quote]
那还要考虑 子文件啊 需要递归寻找。。。
hhc123 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hhc123 的回复:]
FileInfo fi=new FileInfo(path);

string str=fi.Name.SubString(fi.Name.LastOf('.'),fi.Name.length);
[/Quote]
原来我离题
用Directory
Hinagi 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 asxinyu_usst 的回复:]
是不是获取指定路径下 同一类型的所有文件的完整地址啊。。?
[/Quote]

恩,对
qqzeng-ip 2009-12-09
  • 打赏
  • 举报
回复
   string strPath = "~/fffffff/";//取出所在路径
System.IO.File.Exists(strPath + strRFname + ".后缀"
hhc123 2009-12-09
  • 打赏
  • 举报
回复
FileInfo fi=new FileInfo(path);

string str=fi.Name.SubString(fi.Name.LastOf('.'),fi.Name.length);
ztenv 2009-12-09
  • 打赏
  • 举报
回复
Directory.GetFiles("路径名",“匹配模式”);
返回一个字符串数组;
数据之巅 2009-12-09
  • 打赏
  • 举报
回复
是不是获取指定路径下 同一类型的所有文件的完整地址啊。。?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧