这样写吧: string[] res = Directory.GetFiles(filePath,"*."+lastName ); string[] res = Directory.GetFiles(C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\示例图片", "*.jpg");
//递归调用该方法来去子文件中的文件进行搜索 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"); 记住,调用的时候输入的地址后面一定要加一个 "\",或者自己在程序里面改,默认加上也可以
//递归调用该方法来去子文件中的文件进行搜索 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"); 记住,调用的时候输入的地址后面一定要加一个 "\",或者自己在程序里面改,默认加上也可以
返回一个字符串数组;
System.IO.File.Exists(strPath + strRFname + ".后缀"
不用考虑那个了
string[] fl = Directory.GetFiles("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\示例图片", ".jpg");这样写不对么? 为什么取出来fl为空...
string[] res = Directory.GetFiles(filePath,"*."+lastName );
string[] res = Directory.GetFiles(C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\示例图片", "*.jpg");
/// 寻找指定文件夹及其子文件夹中的所有指定类型的文件名称
/// </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");
记住,调用的时候输入的地址后面一定要加一个 "\",或者自己在程序里面改,默认加上也可以
/// 寻找指定文件夹及其子文件夹中的所有指定类型的文件名称
/// </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");
记住,调用的时候输入的地址后面一定要加一个 "\",或者自己在程序里面改,默认加上也可以