/// http://jeanne.wankuma.com/tips/csharp/directory/getfilesmostdeep.html
/// ---------------------------------------------------------------------------------------
/// <summary>
/// 指定した検索パターンに一致するファイルを最下層まで検索しすべて返します。</summary>
/// <param name="stRootPath">
/// 検索を開始する最上層のディレクトリへのパス。</param>
/// <param name="stPattern">
/// パス内のファイル名と対応させる検索文字列。</param>
/// <returns>
/// 検索パターンに一致したすべてのファイルパス。</returns>
/// ---------------------------------------------------------------------------------------
public static string[] GetFilesMostDeep(string stRootPath, string stPattern)
{
// ファイルリスト取得開始をデバッグ出力
Debug.Print(DateTime.Now + " Started to get files recursivery.");
StringCollection hStringCollection = new StringCollection();
// このディレクトリ内のすべてのファイルを検索する
foreach (string stFilePath in Directory.GetFiles(stRootPath, stPattern))
{
hStringCollection.Add(stFilePath);
Debug.Print(DateTime.Now + " Found image file, Filename = " + stFilePath);
}
// このディレクトリ内のすべてのサブディレクトリを検索する (再帰)
foreach (string stDirPath in Directory.GetDirectories(stRootPath))
{
string[] stFilePathes = GetFilesMostDeep(stDirPath, stPattern);
// 条件に合致したファイルがあった場合は、ArrayList に加える
if (stFilePathes != null)
{
hStringCollection.AddRange(stFilePathes);
}
}
// StringCollection を 1 次元の String 配列にして返す
string[] stReturns = new string[hStringCollection.Count];
hStringCollection.CopyTo(stReturns, 0);
// ファイルリスト取得終了をデバッグ出力
Debug.Print(DateTime.Now + " Finished to get files recursivery.");
return stReturns;
}