星期一, 4月 20, 2009

列舉目錄下所有檔案的C# class

剛在噗浪上看到有人花了不少時間寫列舉目錄下所有檔案的class,記得當初鳥毅也在CodeProject上找了一會兒,以下是鳥毅改寫的Code(原始出處忘了留:P ),授權以原始出處為準(逃~)
Update:我改寫的class似乎太像(十年前的)Java,請看回應laneser寫的版本,這才是.Net風格呀。
using System;
using System.Collections;
using System.IO;

namespace tenyi.io
{
public class FileExplorer
{
public ArrayList FileList = new ArrayList();
public ArrayList extensionLists = null;
private string myPath = null;
private bool recursive = true;
private DateTime lastDateTime = new DateTime(1, 1, 1);

public DateTime LastDateTime
{
get { return lastDateTime; }
}

public FileExplorer(string path)
{
myPath = path;
recursive = false;
FileList = GetFiles(myPath);
}

public FileExplorer(string path, string filter)
{
myPath = path;
recursive = false;
FileList = GetFiles(myPath, filter);
}

public FileExplorer(string path, ArrayList extensions)
{
myPath = path;
extensionLists = extensions;
recursive = false;
FileList = GetFiles(myPath);
}
public FileExplorer(string path, ArrayList extensions, string filter)
{
myPath = path;
extensionLists = extensions;
recursive = false;
FileList = GetFiles(myPath, filter);
}


public FileExplorer(string path, bool recursive)
{
myPath = path;
recursive = recursive;
FileList = GetFiles(myPath);
}

public FileExplorer(string path, string filter, bool recursive)
{
myPath = path;
recursive = recursive;
FileList = GetFiles(myPath, filter);
}

public FileExplorer(string path, ArrayList extensions, bool recursive)
{
myPath = path;
extensionLists = extensions;
recursive = recursive;
FileList = GetFiles(myPath);
}

public FileExplorer(string path, ArrayList extensions, bool recursive, DateTime theTime)
{
myPath = path;
extensionLists = extensions;
recursive = recursive;
FileList = GetFiles(theTime, FileDateCompare.GreatThan);
}

public ArrayList GetFiles(string strPath)
{
return GetFiles(strPath, null);
}

public ArrayList GetFiles(string strPath, string filter)
{
DirectoryInfo dir = new DirectoryInfo(strPath);
FileInfo[] files;
DirectoryInfo[] dirs;
ArrayList List = new ArrayList();

//if the source dir doesn't exist, throw
if (! dir.Exists)
{
throw new Exception("Source directory doesn't exist: " + strPath);
}

if(filter == null)
{
//get all files in the current dir
files = dir.GetFiles();
}
else
{
files = dir.GetFiles(filter);
}

foreach (FileInfo file in files)
{
if (extensionLists == null)
{
List.Add(new FileData(file.FullName, file.Name, file.Extension, file.Length, file.LastWriteTime));
}
else
{
if (checkFile(file.Extension))
{
List.Add(new FileData(file.FullName, file.Name, file.Extension, file.Length, file.LastWriteTime));
}
}
}

files = null;

//if not recursive, all work is done
if (! recursive)
{
return List;
}

//otherwise, get dirs
dirs = dir.GetDirectories();

//loop through each sub directory in the current dir
string tempDir;
foreach (DirectoryInfo subdir in dirs)
{
tempDir = subdir.FullName;
ArrayList temp = new ArrayList();
temp = GetFiles(tempDir);
for (int i = 0; i < temp.Count; i++)
{
FileData TempData = (FileData) temp[i];
List.Add(new FileData(TempData.FullName, TempData.Name, TempData.Extension, TempData.Length, TempData.LastWriteTime));
}
}
dirs = null;
dir = null;
return List;
}

public ArrayList GetFiles()
{
return GetFiles(myPath);
}

public ArrayList GetFiles(DateTime dt, FileDateCompare state)
{
DirectoryInfo dir = new DirectoryInfo(myPath);
FileInfo[] files;
DirectoryInfo[] dirs;
ArrayList aList = new ArrayList();

//if the source dir doesn't exist, throw
if (! dir.Exists)
{
throw new Exception("Source directory doesn't exist: " + myPath);
}

//get all files in the current dir
files = dir.GetFiles();

//loop through each file
foreach (FileInfo file in files)
{
if (extensionLists == null)
{
if (file.LastWriteTime.CompareTo(dt) > 0)
{
aList.Add(new FileData(file.FullName, file.Name, file.Extension, file.Length, file.LastWriteTime));
if (file.LastWriteTime.CompareTo(lastDateTime) > 0)
{
lastDateTime = file.LastWriteTime;
}
}
}
else
{
if (checkFile(file.Extension))
{
if (file.LastWriteTime.CompareTo(dt) > 0)
{
aList.Add(new FileData(file.FullName, file.Name, file.Extension, file.Length, file.LastWriteTime));
}
if (file.LastWriteTime.CompareTo(lastDateTime) > 0)
{
lastDateTime = file.LastWriteTime;
}
}
}
}

//cleanup
files = null;

//if not recursive, all work is done
if (! recursive)
{
return aList;
}

//otherwise, get dirs
dirs = dir.GetDirectories();

//loop through each sub directory in the current dir
string tempDir;
foreach (DirectoryInfo subdir in dirs)
{
tempDir = subdir.FullName;
ArrayList temp = new ArrayList();
temp = GetFiles(tempDir);
for (int i = 0; i < temp.Count; i++)
{
FileData TempData = (FileData) temp[i];
aList.Add(new FileData(TempData.FullName, TempData.Name, TempData.Extension, TempData.Length, TempData.LastWriteTime));
}
}

//cleanup
dirs = null;
dir = null;
return aList;
}

public ArrayList GetDirectories()
{
return GetDirectories(myPath, null, recursive);
}

public static ArrayList GetDirectories(string path, ArrayList filter, bool isRecursive)
{
DirectoryInfo thisDirectoryInfo = new DirectoryInfo(path);
ArrayList arrayList = new ArrayList();

DirectoryInfo[] subDirectoryInfos;

//if the source dir doesn't exist, throw
if (! thisDirectoryInfo.Exists)
{
throw new Exception("Source directory doesn't exist: " + path);
}

//get dirs
subDirectoryInfos = thisDirectoryInfo.GetDirectories();

//loop through each sub directory in the current dir
string tempDir;
if (subDirectoryInfos.Length == 0)
{
if (filter == null)
{
arrayList.Add(thisDirectoryInfo);
}
else
{
if (filter.Contains(thisDirectoryInfo.Name))
{
arrayList.Add(thisDirectoryInfo);
}
}
}

foreach (DirectoryInfo subdir in subDirectoryInfos)
{
tempDir = subdir.FullName;
ArrayList temp = new ArrayList();
//if not recursive, all work is done
if (isRecursive)
{
temp = GetDirectories(tempDir, filter, isRecursive);
for (int i = 0; i < temp.Count; i++)
{
arrayList.Add(temp[i]);
}
}
else
{
if (filter == null)
{
arrayList.Add(subdir);
}
else
{
if (filter.Contains(subdir.Name))
{
arrayList.Add(subdir);
}
}
}

}

subDirectoryInfos = null;
thisDirectoryInfo = null;
return arrayList;
}

private bool checkFile(string strExtension)
{
bool throwaway = true;
for (int j = 0; j < extensionLists.Count; j++)
{
if (extensionLists[j].ToString().ToLower() == strExtension.ToLower())
{
throwaway = false;
}
}

if (throwaway)
{
return false;
}
else
{
return true;
}
}
}
}

1 則留言:

Laneser 提到...

獻曝一下, 如果是 VS2008, 我會寫類似這樣的 function:

static IEnumerable<FileInfo>GetFiles(string dir, string pattern, bool isRecursive)
{
var dinfo = new DirectoryInfo(dir);
foreach (var f in dinfo.GetFiles(pattern))
yield return f;
if (isRecursive)
{
foreach (DirectoryInfo subdir in dinfo.GetDirectories())
foreach (FileInfo fi in GetFiles(subdir.FullName, pattern, true))
yield return fi;
}
}