FileSystemInfo.Attributes プロパティ

定義

現在のファイルまたはディレクトリの属性を取得または設定します。

public:
 property System::IO::FileAttributes Attributes { System::IO::FileAttributes get(); void set(System::IO::FileAttributes value); };
public System.IO.FileAttributes Attributes { get; set; }
member this.Attributes : System.IO.FileAttributes with get, set
Public Property Attributes As FileAttributes

プロパティ値

FileAttributes

現在の FileAttributesFileSystemInfo

例外

指定されたファイルが存在しません。 プロパティ値を設定する場合にのみスローされます。

指定されたパスが無効です。 たとえば、マップされていないドライブ上にあります。 プロパティ値を設定する場合にのみスローされます。

呼び出し元に必要なアクセス許可がありません。

.NET Core と .NET 5 以降のみ: ユーザーは属性値の設定を試みますが、書き込みアクセス許可がありません。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

呼び出し元が、無効なファイル属性を設定しようとしました。

または

.NET Framework のみ: ユーザーが属性値を設定しようとしていますが、書き込みアクセス許可がありません。

Refresh() がデータを初期化できません。

Attributesプロパティの例を次に示します。 このコード例は、FileSystemInfo クラスのために提供されている大規模な例の一部です。

static void DisplayFileSystemInfoAttributes(FileSystemInfo^ fsi)
{
    //  Assume that this entry is a file.
    String^ entryType = "File";

    // Determine if entry is really a directory
    if ((fsi->Attributes & FileAttributes::Directory) == FileAttributes::Directory)
    {
        entryType = "Directory";
    }
    //  Show this entry's type, name, and creation date.
    Console::WriteLine("{0} entry {1} was created on {2:D}", entryType, fsi->FullName, fsi->CreationTime);
}
static void DisplayFileSystemInfoAttributes(FileSystemInfo fsi)
{
    //  Assume that this entry is a file.
    string entryType = "File";

    // Determine if entry is really a directory
    if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory )
    {
        entryType = "Directory";
    }
    //  Show this entry's type, name, and creation date.
    Console.WriteLine("{0} entry {1} was created on {2:D}", entryType, fsi.FullName, fsi.CreationTime);
}
Sub DisplayFileSystemInfoAttributes(ByVal fsi As IO.FileSystemInfo)
    ' Assume that this entry is a file.
    Dim entryType As String = "File"

    ' Determine if this entry is really a directory.
    If (fsi.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
        entryType = "Directory"
    End If

    ' Show this entry's type, name, and creation date.
    Console.WriteLine("{0} entry {1} was created on {2:D}", _
        entryType, fsi.FullName, fsi.CreationTime)
End Sub

注釈

オブジェクトの現在のAttributesインスタンスが次DirectoryInfoのいずれかのメソッドから返された場合、プロパティのFileSystemInfo値は事前にキャッシュされます。

値自体または他 FileSystemInfo のプロパティにアクセスすると、値がキャッシュされる可能性があります。 最新の値を取得するには、メソッドを Refresh 呼び出します。

最後にキャッシュされた状態のパスが存在しない場合、戻り値は (FileAttributes)(-1). FileNotFoundException または DirectoryNotFoundException 、値を設定するときにのみスローできます。

このプロパティの値は、アーカイブ、圧縮、ディレクトリ、非表示、オフライン、読み取り専用、システム、および一時ファイルの属性フラグの組み合わせです。

この値を設定する場合は、ビットごとの OR 演算子 (| C# または Or Visual Basic の場合) を使用して、複数の値を適用します。 プロパティ内の既存の値を Attributes 保持するには、プロパティの値を Attributes 割り当てに含めます。 次に例を示します。

exampleFile.Attributes = exampleFile.Attributes | FileAttributes.ReadOnly;

適用対象

こちらもご覧ください