C# File SetAttributes

Description

File SetAttributes Sets the specified FileAttributes of the file on the specified path.

Syntax

File.SetAttributes has the following syntax.


public static void SetAttributes(
  string path,
  FileAttributes fileAttributes
)

Parameters

File.SetAttributes has the following parameters.

  • path - The path to the file.
  • fileAttributes - A bitwise combination of the enumeration values.

Returns

File.SetAttributes method returns

Example

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


using System;// w w  w. j  ava 2 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