13,009
回編集
342行目: | 342行目: | ||
{ | { | ||
Console.WriteLine($"エラー: アクセス権限がない {e.Message}"); | Console.WriteLine($"エラー: アクセス権限がない {e.Message}"); | ||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== ディレクトリの検索 == | |||
ディレクトリの検索は、権限の問題やディレクトリが見つからない場合のエラーが発生する可能性があるため、エラーハンドリングが重要である。<br> | |||
<br> | |||
<u>※注意</u><br> | |||
<u>Linuxのファイルシステムでは大文字と小文字が区別されることに注意する。</u><br> | |||
<u>そのため、パターンマッチングを使用する場合は、大文字小文字の違いに気を付ける必要がある。</u><br> | |||
<br> | |||
==== 全てのサブディレクトリの再帰的取得 ==== | |||
以下の例では、指定したディレクトリ以下の全てのサブディレクトリを取得している。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
class Program | |||
{ | |||
static void Main(string[] args) | |||
{ | |||
string path = "/home/user"; | |||
try | |||
{ | |||
// 指定したディレクトリ内の全てのサブディレクトリを再帰的に取得 | |||
string[] directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); | |||
foreach (string dir in directories) | |||
{ | |||
Console.WriteLine(dir); | |||
} | |||
} | |||
catch (UnauthorizedAccessException e) | |||
{ | |||
Console.WriteLine($"エラー: アクセス権限がない {e.Message}"); | |||
} | |||
catch (DirectoryNotFoundException e) | |||
{ | |||
Console.WriteLine($"エラー: 指定されたディレクトリが存在しない {e.Message}"); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== パターンマッチングを使用したディレクトリ検索 ==== | |||
パターンマッチングを使用した方法は、特定のパターンに一致するディレクトリのみを検索する。<br> | |||
これは、特定の名前や形式のディレクトリを探す場合に便利である。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
class Program | |||
{ | |||
static void Main(string[] args) | |||
{ | |||
string path = "/home/user"; | |||
string searchPattern = "sample*"; // "sample"で始まるディレクトリを検索 | |||
try | |||
{ | |||
// 指定したパターンに一致するディレクトリを検索 | |||
string[] directories = Directory.GetDirectories(path, searchPattern, SearchOption.AllDirectories); | |||
foreach (string dir in directories) | |||
{ | |||
Console.WriteLine(dir); | |||
} | |||
} | |||
catch (UnauthorizedAccessException e) | |||
{ | |||
Console.WriteLine($"エラー: アクセス権限がない {e.Message}"); | |||
} | |||
catch (DirectoryNotFoundException e) | |||
{ | |||
Console.WriteLine($"エラー: 指定されたディレクトリが存在しない {e.Message}"); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 正規表現を使用したディレクトリ検索 ==== | |||
正規表現を使用したディレクトリ検索は強力であるが、適切に使用しないとパフォーマンスの問題を引き起こす可能性がある。<br> | |||
使用する場合は、検索対象のディレクトリ構造の規模と正規表現の複雑さを考慮する必要がある。<br> | |||
<br> | |||
また、正規表現で大文字と小文字を区別しない検索を行う場合は、<code>RegexOptions.IgnoreCase</code>オプションを使用する。<br> | |||
<br> | |||
以下の例では、正規表現を使用してディレクトリを検索している。<br> | |||
<br> | |||
# Regex.IsMatchメソッドを使用して、各ディレクトリ名が指定された正規表現パターンに一致するかどうかを確認する。 | |||
# Path.GetFileNameメソッドを使用して、フルパスからディレクトリ名のみを抽出する。 | |||
# これにより、パス全体ではなくディレクトリ名のみに対して正規表現マッチングを行うことができる。 | |||
<br> | |||
ただし、大規模なディレクトリ構造を扱う場合は非同期処理を使用することを推奨する。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
using System.Text.RegularExpressions; | |||
class Program | |||
{ | |||
static void Main(string[] args) | |||
{ | |||
string path = "/home/user"; | |||
string pattern = @"^sample_\d+$"; // "sample_"で始まり、その後に数字が続くディレクトリを検索 | |||
try | |||
{ | |||
// 指定したディレクトリ内のすべてのサブディレクトリを取得 | |||
string[] allDirectories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); | |||
// 正規表現パターンに一致するディレクトリをフィルタリング | |||
var matchingDirectories = Array.FindAll(allDirectories, dir => Regex.IsMatch(Path.GetFileName(dir), pattern)); | |||
foreach (string dir in matchingDirectories) | |||
{ | |||
Console.WriteLine(dir); | |||
} | |||
} | |||
catch (UnauthorizedAccessException e) | |||
{ | |||
Console.WriteLine($"エラー: アクセス権限がない {e.Message}"); | |||
} | |||
catch (DirectoryNotFoundException e) | |||
{ | |||
Console.WriteLine($"エラー: 指定されたディレクトリが存在しない {e.Message}"); | |||
} | |||
catch (RegexMatchTimeoutException e) | |||
{ | |||
Console.WriteLine($"エラー: 正規表現のマッチングがタイムアウト {e.Message}"); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 非同期処理で検索 ==== | |||
大規模なディレクトリ構造を扱う場合は時間が掛かる可能性がある。<br> | |||
非同期処理を行うことにより、大規模なディレクトリ構造を扱う場合にアプリケーションの応答性を維持するのに役立つ。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
using System.Linq; | |||
using System.Text.RegularExpressions; | |||
using System.Threading.Tasks; | |||
class Program | |||
{ | |||
static async Task Main(string[] args) | |||
{ | |||
string path = "/home/user"; | |||
string pattern = @"^test_\d+$"; | |||
try | |||
{ | |||
var matchingDirectories = await Task.Run(() => | |||
{ | |||
return Directory.GetDirectories(path, "*", SearchOption.AllDirectories) | |||
.Where(dir => Regex.IsMatch(Path.GetFileName(dir), pattern)) | |||
.ToArray(); | |||
}); | |||
foreach (string dir in matchingDirectories) | |||
{ | |||
Console.WriteLine(dir); | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine($"エラーが発生: {e.Message}"); | |||
} | } | ||
} | } |