Muokkaa

Jaa


FileSystemInfo.Attributes Property

Definition

Gets or sets the attributes for the current file or directory.

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

Property Value

FileAttributes of the current FileSystemInfo.

Exceptions

The specified file doesn't exist. Only thrown when setting the property value.

The specified path is invalid. For example, it's on an unmapped drive. Only thrown when setting the property value.

The caller doesn't have the required permission.

.NET Core and .NET 5+ only: The user attempts to set an attribute value but doesn't have write permission.

The specified path, file name, or both exceed the system-defined maximum length.

The caller attempts to set an invalid file attribute.

-or-

.NET Framework only: The user attempts to set an attribute value but doesn't have write permission.

Refresh() cannot initialize the data.

Examples

The following example demonstrates the Attributes property. This code example is part of a larger example provided for the FileSystemInfo class.

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

Remarks

The value of the Attributes property is pre-cached if the current instance of the FileSystemInfo object was returned from any of the following DirectoryInfo methods:

The value may be cached when either the value itself or other FileSystemInfo properties are accessed. To get the latest value, call the Refresh method.

If the path doesn't exist as of the last cached state, the return value is (FileAttributes)(-1). FileNotFoundException or DirectoryNotFoundException can only be thrown when setting the value.

The value of this property is a combination of the archive, compressed, directory, hidden, offline, read-only, system, and temporary file attribute flags.

When you set this value, use the bitwise OR operator (| in C# or Or in Visual Basic) to apply more than one value. To retain any existing values in the Attributes property, include the value of the Attributes property in your assignment. For example:

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

Applies to

See also