跳到主要內容

列舉目錄下所有檔案的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;
}
}
}
}

留言

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;
}
}

這個網誌中的熱門文章

自然人憑證讀卡機驅動程式

鳥毅用的是第一代的自然人憑證讀卡機,EZ100PU(後來有同事買EZmini可以讀SIM卡似乎更好),每年報稅時用一次。 本來只是要申請些政府業務,一時之間找不到光碟,沒想到在 驅動程式下載 居然看到Linux和Mac的驅動程式,剩下的就是政府單位的網頁和程式應該改版了吧!!!

DBeaver 介面語言

DBeaver是我個人頗常用的一套跨平台Database管理工具,最近升級後發現Windows版本居然變成簡體中文,而且無法切換為英文。

如何將較高版本SQL Server複製到低版本SQL Server (降級為舊版)並保留權限及資料庫圖表

一般若是要將SQL Server裡的Database轉往其他Server時,最簡單的方式就是備份(Backup)後再還原(Restore),或者是䣃離(detach)後附加(attach)。 但是很不幸地,若是由較低版本(e.g. 2008)到較高版本(e.g. 2012)要怎麼辦呢?