.NET Framework Class Library
File..::.GetAttributes Method

Gets the FileAttributes of the file on the path.

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

Visual Basic (Declaration)
Public Shared Function GetAttributes ( _
    path As String _
) As FileAttributes
Visual Basic (Usage)
Dim path As String
Dim returnValue As FileAttributes

returnValue = File.GetAttributes(path)
C#
public static FileAttributes GetAttributes(
    string path
)
Visual C++
public:
static FileAttributes GetAttributes(
    String^ path
)
JScript
public static function GetAttributes(
    path : String
) : FileAttributes

Parameters

path
Type: System..::.String
The path to the file.

Return Value

Type: System.IO..::.FileAttributes
The FileAttributes of the file on the path.
Exceptions

ExceptionCondition
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.

FileNotFoundException

path represents a file and is invalid, such as being on an unmapped drive, or the file cannot be found.

DirectoryNotFoundException

path represents a directory and is invalid, such as being on an unmapped drive, or the directory cannot be found.

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 a list of common I/O tasks, see Common I/O Tasks.

Examples

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"
        ' Create 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";

        // Create 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);
        }
    }
}
Visual C++
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";

   // Create 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, static_cast<FileAttributes>(File::GetAttributes( path ) | FileAttributes::Hidden) );
      Console::WriteLine( "The {0} file is now hidden.", path );
   }
}

.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

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
See Also

Reference

Other Resources

Tags :


Community Content

David M. Kean - MSFT
The correct way of removing an attribute from a file

Although the above samples shows you one way of removing the FileAttributes.Hidden attribute, it also, as a side-effect, removes any other attribute (such as FileAttributes.ReadOnly) from the file. The correct way is to only remove the attribute you are interested in.

The following example shows a way of doing this by removing the FileAttributes.ReadOnly attribute.

[C#]
 
using System;
using System.IO;
 
namespace Samples
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Temp\MyTest.txt";
    
            FileAttributes attributes = File.GetAttributes(path);
            if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
// File is hidden, so remove the hidden attribute
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
 
                File.SetAttributes(path, attributes);

}
}
    
        private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
}
Tags :

Page view tracker