public class FileAccess
{數組
//儲存全部文件夾名
private ArrayList dirs;string
public FileAccess()
{
dirs = new ArrayList();
}class
//獲取全部文件名
private ArrayList GetFileName(string dirPath)
{
ArrayList list = new ArrayList();object
if (Directory.Exists(dirPath))
{
list.AddRange(Directory.GetFiles(dirPath));
}
return list;
}foreach
//獲取全部文件夾及子文件夾
private void GetDirs(string dirPath)
{
if (Directory.GetDirectories(dirPath).Length > 0)
{
foreach (string path in Directory.GetDirectories(dirPath))
{
dirs.Add(path);
GetDirs(path);
}
}
}List
/// <summary>
/// 獲取給出文件夾及其子文件夾下的全部文件名
/// (文件名爲路徑加文件名及後綴,
/// 使用的時候GetAllFileName().ToArray()方法能夠轉換成object數組
/// 以後再ToString()分別獲得文件名)
/// </summary>
/// <param name="rootPath">文件夾根目錄</param>
/// <returns></returns>
public ArrayList GetAllFileName(string rootPath)
{
dirs.Add(rootPath);
GetDirs(rootPath);
object[] allDir = dirs.ToArray();方法
ArrayList list = new ArrayList();di
foreach (object o in allDir)
{
list.AddRange(GetFileName(o.ToString()));
}文件
return list;
}new
/// <summary>
/// 若是上個方法不知道怎麼用,那就調用這個方法吧
/// </summary>
/// <param name="rootPath"></param>
/// <returns></returns>
public List<string> FileName(string rootPath)
{
List<string> list = new List<string>();
foreach (object o in GetAllFileName(rootPath).ToArray()) { list.Add(o.ToString()); } return list; }}