「C Sharpの基礎 - ディレクトリ」の版間の差分

ナビゲーションに移動 検索に移動
 
753行目: 753行目:
                                         UnixFileMode.OtherRead);
                                         UnixFileMode.OtherRead);
           });
           });
      }
    }
}
</syntaxhighlight>
<br><br>
== ディレクトリの属性 ==
Linuxでは、Windowsのような詳細な属性 (アーカイブ、システム、隠しファイル等) は限られているが、基本的な属性や時間情報等を取得および設定することができる。<br>
<br>
<code>DirectoryInfo</code>クラスを使用して、ディレクトリの情報や属性にアクセスする。<br>
また、属性の設定には適切な権限が必要であるため、エラーハンドリングが重要となる。<br>
<br>
時間情報 (作成日時、最終アクセス日時、最終更新日時) の変更が可能であるが、これらの操作はファイルシステムの動作に影響を与える可能性があるため、慎重に行う必要がある。<br>
<br>
<code>FileAttributes</code>列挙型を使用して属性を設定するが、Linuxでは一部の属性 (例: ReadOnly) のみが有効である。<br>
<br>
<u>※注意</u><br>
<u>不適切な属性の変更は、システムの動作に影響を与える可能性があるため、ファイルシステムの整合性とセキュリティに注意すること。</u><br>
<br>
==== ディレクトリの属性の取得 ====
以下の例では、Linuxにおいて、ディレクトリの基本的な属性を取得している。<br>
<br>
<syntaxhighlight lang="c#">
using System;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
      string directoryPath = "/home/user/testdir";
      try
      {
          DirectoryInfo dirInfo = new DirectoryInfo(directoryPath);
          if (dirInfo.Exists)
          {
            Console.WriteLine($"ディレクトリ: {dirInfo.FullName}");
            Console.WriteLine($"作成日時: {dirInfo.CreationTime}");
            Console.WriteLine($"最終アクセス日時: {dirInfo.LastAccessTime}");
            Console.WriteLine($"最終更新日時: {dirInfo.LastWriteTime}");
            Console.WriteLine($"属性: {dirInfo.Attributes}");
            // Linuxパーミッションの取得
            UnixFileMode mode = File.GetUnixFileMode(directoryPath);
            Console.WriteLine($"Unixパーミッション: {Convert.ToString((int)mode, 8).PadLeft(4, '0')}");
          }
          else
          {
            Console.WriteLine($"ディレクトリ '{directoryPath}' が存在しない");
          }
      }
      catch (UnauthorizedAccessException e)
      {
          Console.WriteLine($"エラー: アクセス権限がない {e.Message}");
      }
      catch (IOException e)
      {
          Console.WriteLine($"エラー: I/O操作中にエラーが発生 {e.Message}");
      }
    }
}
</syntaxhighlight>
<br>
==== ディレクトリの属性の設定 ====
以下の例では、ディレクトリの属性を設定している。<br>
<br>
<syntaxhighlight lang="c#">
using System;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
      string directoryPath = "/home/user/testdir";
      try
      {
          DirectoryInfo dirInfo = new DirectoryInfo(directoryPath);
          if (dirInfo.Exists)
          {
            // 最終アクセス日時と最終更新日時を設定
            DateTime newTime = DateTime.Now;
            dirInfo.LastAccessTime = newTime;
            dirInfo.LastWriteTime = newTime;
            // 属性を設定(例:読み取り専用)
            dirInfo.Attributes |= FileAttributes.ReadOnly;
            // 更新後の属性を表示
            Console.WriteLine($"最終アクセス日時: {dirInfo.LastAccessTime}");
            Console.WriteLine($"最終更新日時: {dirInfo.LastWriteTime}");
            Console.WriteLine($"属性: {dirInfo.Attributes}");
          }
          else
          {
            Console.WriteLine($"ディレクトリ '{directoryPath}' が存在しない");
          }
      }
      catch (UnauthorizedAccessException e)
      {
          Console.WriteLine($"エラー: アクセス権限がない {e.Message}");
      }
      catch (IOException e)
      {
          Console.WriteLine($"エラー: I/O操作中にエラーが発生 {e.Message}");
      }
    }
}
</syntaxhighlight>
<br>
==== ディレクトリの属性の設定 (非同期処理) ====
以下の例では、非同期処理を使用して、ディレクトリの属性を設定している。<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
      {
          DirectoryInfo dirInfo = new DirectoryInfo(directoryPath);
          if (dirInfo.Exists)
          {
            await Task.Run(() =>
            {
                DateTime newTime = DateTime.Now;
                dirInfo.LastAccessTime = newTime;
                dirInfo.LastWriteTime = newTime;
                dirInfo.Attributes |= FileAttributes.ReadOnly;
            });
          }
          else
          {
            Console.WriteLine($"ディレクトリ '{directoryPath}' が存在しない");
          }
      }
      catch (Exception e)
      {
          Console.WriteLine($"エラーが発生: {e.Message}");
       }
       }
     }
     }

案内メニュー