.NET Framework Class Library
FileSystemInfo..::.Attributes Property

Gets or sets the current directory or file.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Property Attributes As FileAttributes
Visual Basic (Usage)
Dim instance As FileSystemInfo
Dim value As FileAttributes

value = instance.Attributes

instance.Attributes = value
C#
public FileAttributes Attributes { get; set; }
Visual C++
public:
property FileAttributes Attributes {
    FileAttributes get ();
    void set (FileAttributes value);
}
JScript
public function get Attributes () : FileAttributes
public function set Attributes (value : FileAttributes)

Property Value

Type: System.IO..::.FileAttributes
FileAttributes of the current FileSystemInfo.
Exceptions

ExceptionCondition
FileNotFoundException

The specified file does not exist.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

SecurityException

The caller does not have the required permission.

ArgumentException

The caller attempts to set an invalid file attribute.

IOException

Refresh cannot initialize the data.

Remarks

When first called, FileSystemInfo calls Refresh and returns the cached information on APIs to get attributes and so on. On subsequent calls, you must call Refresh to get the latest copy of the information.

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

For a list of common I/O tasks, see Common I/O Tasks.

Examples

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

Visual Basic
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 = 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
C#
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)
    {
        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);
}
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Community Content

PolishPaul
Dealing with multiple attributes in one file
The above example only works if the file has only 1 attribute. If it has multiple attributes like (hidden | archive | system) this will not work:

file.attribute == FileAttributes.Hidden

since the file.attribute's value will be (hidden | archive | system).

The following code will work instead.

foreach (FileInfo file in fileList)

{


if ( ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) ||

( (file.Attributes &

FileAttributes.System) == FileAttributes.System) ||

( (file.Attributes &

FileAttributes.ReadOnly) == FileAttributes.ReadOnly) ||

( (file.Attributes &

FileAttributes.Directory) == FileAttributes.Directory) )

{

currentFileListTotalSize += file.Length;

}

}


This example looks to see if the file has one of the above attributes and will be true if just one of them exists.

So the first line:
(file.Attributes & FileAttributes.Hidden)
looks to see if the attribute HIDDEN is in the list of that particular file's attributes and if so, the statement returns "FileAttributes.Hidden" which can then be == to FileAttributes.Hidden as above, resulting in a TRUE value; Otherwise the result is 0.

Another simpler example:

if ( file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
// file has a hidden attribute, but maybe others too
}else
{
// file does NOT have a hidded attribute, but may have others
}






PolishPaul
Dealing with multiple attributes in one file
The above example only works if the file has only 1 attribute. If it has multiple attributes like (hidden | archive | system) this will not work:

file.attribute == FileAttributes.Hidden

since the file.attribute's value will be (hidden | archive | system).

The following code will work instead.

foreach (FileInfo file in fileList)

{


if ( ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) ||

( (file.Attributes &

FileAttributes.System) == FileAttributes.System) ||

( (file.Attributes &

FileAttributes.ReadOnly) == FileAttributes.ReadOnly) ||

( (file.Attributes &

FileAttributes.Directory) == FileAttributes.Directory) )

{

currentFileListTotalSize += file.Length;

}

}


This example looks to see if the file has one of the above attributes and will be true if just one of them exists.

So the first line:
(file.Attributes & FileAttributes.Hidden)
looks to see if the attribute HIDDEN is in the list of that particular file's attributes and if so, the statement returns "FileAttributes.Hidden" which can then be == to FileAttributes.Hidden as above, resulting in a TRUE value; Otherwise the result is 0.

Another simpler example:

if ( file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
// file has a hidden attribute, but maybe others too
}else
{
// file does NOT have a hidded attribute, but may have others
}

VBRocks
Working with Multiple Attributes in VB.NET by VBRocks

Here's an alternative approach:


'Get all of the files on the C:\

Dim fileList As IO.FileInfo() = New IO.DirectoryInfo("C:\").GetFiles()


'Stores a file's attributes as a string

Dim atts AsString = String.Empty



'Files marked as Hidden

'------------------------------

Debug.WriteLine("Files marked as Hidden")

Debug.WriteLine(NewString("-"c, 30))


'Loop through each file in the list (on C:\)

ForEach file As IO.FileInfo In fileList

'Get the file's attributes
atts = file.Attributes.ToString()


'View only Hidden Files
If atts.IndexOf(IO.FileAttributes.Hidden.ToString()) > -1 Then _
Debug.WriteLine(file.Name.PadRight(30) & file.Attributes.ToString())

Next



'Files marked as Hidden, System or Archive

'------------------------------

Debug.WriteLine(System.Environment.NewLine)

Debug.WriteLine("Files marked as Hidden, System or Archive")

Debug.WriteLine(NewString("-"c, 30))


'Loop through each file in the list (on C:\)

ForEach file As IO.FileInfo In fileList

'Get the file's attributes
atts = file.Attributes.ToString()


'View Hidden, System, Archive (Use the enumerator's .ToString() method)
If atts.IndexOf(IO.FileAttributes.Hidden.ToString()) > -1 OrElse _
atts.IndexOf(IO.FileAttributes.System.ToString()) > -1 OrElse _
atts.IndexOf(IO.FileAttributes.Archive.ToString()) > -1 Then

Debug.WriteLine(file.Name.PadRight(30) & file.Attributes.ToString())

End If

Next



Stop'View Output Window

Tags :

Page view tracker