http://www.cnblogs.com/xiaobaigang/archive/2007/10/31/944769.html
代码二:用字符串保存指定后缀名的文件,最后用分格符分割成数组,再循环输出或绑定
递归类
前台调用代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Collections.Generic;
public partial class FindTxt : System.Web.UI.Page
{
System.Collections.ArrayList alst;
protected void Page_Load(object sender, EventArgs e)
{
FindTxt t = new FindTxt();
foreach (string f in t.readlist(Server.MapPath(@"xiaobaigang")))//xiaobaigang为文件夹名称
{
Response.Write(f);
//this.ListBox1.Items.Add(f);
}
}
public void GetFiles(string dir)
{
try
{
string[] files = Directory.GetFiles(dir);//得到文件
foreach (string file in files)//循环文件
{
string exname = file.Substring(file.LastIndexOf(".") + 1);//得到后缀名
// if (".txt|.aspx".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)//查找.txt .aspx结尾的文件
if (".txt".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)//如果后缀名为.txt文件
{
FileInfo fi = new FileInfo(file);//建立FileInfo对象
alst.Add(fi.FullName);//把.txt文件全名加人到FileInfo对象
}
}
}
catch
{
}
}
public string[] readlist(string path)
{
alst = new System.Collections.ArrayList();//建立ArrayList对象
GetDirs(path);//得到文件夹
return (string[])alst.ToArray(typeof(string));//把ArrayList转化为string[]
}
public void GetDirs(string d)//得到所有文件夹
{
GetFiles(d);//得到所有文件夹里面的文件
try
{
string[] dirs = Directory.GetDirectories(d);
foreach (string dir in dirs)
{
GetDirs(dir);//递归
}
}
catch
{
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Collections.Generic;
public partial class FindTxt : System.Web.UI.Page
{
System.Collections.ArrayList alst;
protected void Page_Load(object sender, EventArgs e)
{
FindTxt t = new FindTxt();
foreach (string f in t.readlist(Server.MapPath(@"xiaobaigang")))//xiaobaigang为文件夹名称
{
Response.Write(f);
//this.ListBox1.Items.Add(f);
}
}
public void GetFiles(string dir)
{
try
{
string[] files = Directory.GetFiles(dir);//得到文件
foreach (string file in files)//循环文件
{
string exname = file.Substring(file.LastIndexOf(".") + 1);//得到后缀名
// if (".txt|.aspx".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)//查找.txt .aspx结尾的文件
if (".txt".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)//如果后缀名为.txt文件
{
FileInfo fi = new FileInfo(file);//建立FileInfo对象
alst.Add(fi.FullName);//把.txt文件全名加人到FileInfo对象
}
}
}
catch
{
}
}
public string[] readlist(string path)
{
alst = new System.Collections.ArrayList();//建立ArrayList对象
GetDirs(path);//得到文件夹
return (string[])alst.ToArray(typeof(string));//把ArrayList转化为string[]
}
public void GetDirs(string d)//得到所有文件夹
{
GetFiles(d);//得到所有文件夹里面的文件
try
{
string[] dirs = Directory.GetDirectories(d);
foreach (string dir in dirs)
{
GetDirs(dir);//递归
}
}
catch
{
}
}
}
代码二:用字符串保存指定后缀名的文件,最后用分格符分割成数组,再循环输出或绑定
递归类
using System;
using System.Web;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Text;
using System.IO;
namespace TestGetFiles
{
/// <summary>
/// bsGetFiles 的摘要描述。
/// </summary>
public class bsGetFiles
{
public bsGetFiles()
{
}
private static string result = "";
/// <summary>
/// 得某文件夹下所有的文件
/// </summary>
/// <param name="directory">文件夹名称</param>
/// <param name="pattern">搜寻指类型</param>
/// <returns></returns>
public static string GetFiles(DirectoryInfo directory, string pattern)
{
if(directory.Exists || pattern.Trim()!=string.Empty)
{
foreach(FileInfo info in directory.GetFiles(pattern))
{
result = result + info.FullName.ToString() + ";";
//result = result + info.Name.ToString() + ";";
}
foreach(DirectoryInfo info in directory.GetDirectories())
{
GetFiles(info,pattern);
}
}
string returnString = result;
return returnString;
}
}
}
using System.Web;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Text;
using System.IO;
namespace TestGetFiles
{
/// <summary>
/// bsGetFiles 的摘要描述。
/// </summary>
public class bsGetFiles
{
public bsGetFiles()
{
}
private static string result = "";
/// <summary>
/// 得某文件夹下所有的文件
/// </summary>
/// <param name="directory">文件夹名称</param>
/// <param name="pattern">搜寻指类型</param>
/// <returns></returns>
public static string GetFiles(DirectoryInfo directory, string pattern)
{
if(directory.Exists || pattern.Trim()!=string.Empty)
{
foreach(FileInfo info in directory.GetFiles(pattern))
{
result = result + info.FullName.ToString() + ";";
//result = result + info.Name.ToString() + ";";
}
foreach(DirectoryInfo info in directory.GetDirectories())
{
GetFiles(info,pattern);
}
}
string returnString = result;
return returnString;
}
}
}
前台调用代码
private void button1_Click(object sender, System.EventArgs e)
{
string file = bsGetFiles.GetFiles(new DirectoryInfo(@"D:\TestGetFiles\TestGetFiles\xiaobaigang"),"*.txt");
this.textBox1.Text = file;
}
{
string file = bsGetFiles.GetFiles(new DirectoryInfo(@"D:\TestGetFiles\TestGetFiles\xiaobaigang"),"*.txt");
this.textBox1.Text = file;
}