📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 693行目: | 693行目: | ||
UnixFileMode.GroupRead | | UnixFileMode.GroupRead | | ||
UnixFileMode.OtherRead); | UnixFileMode.OtherRead); | ||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 再帰的なパーミッションの設定 (非同期処理) ==== | |||
以下の例では、非同期処理を使用して、再帰的にディレクトリとそのサブディレクトリのパーミッションを設定している。<br> | |||
大規模なディレクトリ構造を扱う場合は、非同期処理を使用することを推奨する。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
using System.Threading.Tasks; | |||
class Program | |||
{ | |||
static async Task Main(string[] args) | |||
{ | |||
string directoryPath = "/home/user/testdir"; | |||
try | |||
{ | |||
await SetPermissionsRecursivelyAsync(directoryPath); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine($"エラーが発生: {e.Message}"); | |||
} | |||
} | |||
static async Task SetPermissionsRecursivelyAsync(string path) | |||
{ | |||
await Task.Run(() => | |||
{ | |||
// ディレクトリのパーミッションを755に設定 (例: 755 - rwxr-xr-x) | |||
File.SetUnixFileMode(path, UnixFileMode.UserRead | | |||
UnixFileMode.UserWrite | | |||
UnixFileMode.UserExecute | | |||
UnixFileMode.GroupRead | | |||
UnixFileMode.GroupExecute | | |||
UnixFileMode.OtherRead | | |||
UnixFileMode.OtherExecute); | |||
}); | |||
// サブディレクトリに対して再帰的に処理 | |||
foreach (string subDir in Directory.GetDirectories(path)) | |||
{ | |||
await SetPermissionsRecursivelyAsync(subDir); | |||
} | |||
// ファイルのパーミッションも644に設定する場合 (例: 644 - rw-r--r--) | |||
foreach (string file in Directory.GetFiles(path)) | |||
{ | |||
await Task.Run(() => | |||
{ | |||
File.SetUnixFileMode(file, UnixFileMode.UserRead | | |||
UnixFileMode.UserWrite | | |||
UnixFileMode.GroupRead | | |||
UnixFileMode.OtherRead); | |||
}); | |||
} | } | ||
} | } | ||