Export (0) Print
Expand All

File.GetAttributes Method

Gets the FileAttributes of the file on the path.

[Visual Basic]
Public Shared Function GetAttributes( _
   ByVal path As String _
) As FileAttributes
[C#]
public static FileAttributes GetAttributes(
 string path
);
[C++]
public: static FileAttributes GetAttributes(
 String* path
);
[JScript]
public static function GetAttributes(
   path : String
) : FileAttributes;

Parameters

path
The path to the file.

Return Value

The FileAttributes of the file on the path, or -1 if the path or file is not found.

Exceptions

Exception Type Condition
ArgumentException path is empty, contains only white spaces, or contains invalid characters.
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
NotSupportedException path is in an invalid format.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.

Remarks

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file. File.Move

FileInfo.MoveTo

Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Set file attributes. SetAttributes

Example

[Visual Basic, C#, C++] The following example demonstrates the GetAttributes and SetAttributes methods by applying the Archive and Hidden attributes to a file.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        ' Delete the file if it exists.
        If File.Exists(path) = False Then
            File.Create(path)
        End If

        If (File.GetAttributes(path) And FileAttributes.Hidden) = FileAttributes.Hidden Then
            ' Show the file.
            File.SetAttributes(path, FileAttributes.Archive)
            Console.WriteLine("The {0} file is no longer hidden.", path)
        Else
            ' Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.Hidden)
            Console.WriteLine("The {0} file is now hidden.", path)
        End If
    End Sub
End Class

[C#] 
using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        // Delete the file if it exists.
        if (!File.Exists(path)) 
        {
            File.Create(path);
        }

        if ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden) 
        {
            // Show the file.
            File.SetAttributes(path, FileAttributes.Archive);
            Console.WriteLine("The {0} file is no longer hidden.", path);
        } 
        else 
        {
            // Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    // Delete the file if it exists.
    if (!File::Exists(path)) {
        File::Create(path);
    }

    if ((File::GetAttributes(path) & FileAttributes::Hidden) == FileAttributes::Hidden) {
        // Show the file.
        File::SetAttributes(path, FileAttributes::Archive);
        Console::WriteLine(S"The {0} file is no longer hidden.", path);
    } else {
        // Hide the file.
        File::SetAttributes(path, static_cast<FileAttributes>(File::GetAttributes(path) | FileAttributes::Hidden));
        Console::WriteLine(S"The {0} file is now hidden.", path);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

File Class | File Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft