これは私のメソッドのコードです:
t = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string[] textfiles = ApplyAllFiles(t, "*.txt", ProcessFile).ToArray();
それから私もしました:
s = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string[] textfiles = ApplyAllFiles(s, "*.jpg", ProcessFile).ToArray();
ApplyAllFiles メソッド:
static void ProcessFile(string path) {/* ... */}
static IEnumerable<string> ApplyAllFiles(string folder, string searchPattern, Action<string> fileAction)
{
IEnumerable<string> files = Directory.GetFiles(folder, searchPattern);
foreach (string file in files)
{
fileAction(file);
}
foreach (string subDir in Directory.GetDirectories(folder))
{
try
{
files = files.Concat(ApplyAllFiles(subDir, searchPattern, fileAction));
}
catch
{
// swallow, log, whatever
}
}
return files;
}
たとえば、最初のメイン ディレクトリは C:\Users\bout0_000\Documents です。このディレクトリには、いくつかのテキスト ファイルがあります。Documentsのサブ
ディレクトリからすべてのテキスト ファイルを取得しますが、C:\Users\bout0_000\Documents にあるテキスト ファイルは取得しません。たとえば、「* .jpg」を実行した場合、複数の拡張子を取得するにはどうすればよいですか。bmp ファイルの png ファイルと gif ファイルも取得したいと考えています。