C# File GetAttributes

Description

File GetAttributes Gets the FileAttributes of the file on the path.

Syntax

File.GetAttributes has the following syntax.


public static FileAttributes GetAttributes(
  string path
)

Parameters

File.GetAttributes has the following parameters.

  • path - The path to the file.

Returns

File.GetAttributes method returns The FileAttributes of the file on the path.

Example

The following example demonstrates the GetAttributes and SetAttributes methods by applying the Archive and Hidden attributes to a file.


using System;//from   w  ww  .j  ava2  s. c  om
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        FileAttributes attributes = File.GetAttributes(path);

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

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




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter